- How do you declare an integer array of size 5 in C++?
- a) int arr[5];
- b) int arr();
- c) array<int> arr(5);
- d) int arr;
- What is the index of the first element in a C++ array?
- a) 0
- b) 1
- c) -1
- d) Depends on the array size
- How do you access the third element of an array named
arr
?
- a) arr[3]
- b) arr(2)
- c) arr[2]
- d) arr{3}
- What happens if you access an array element outside its declared size?
- a) Compile-time error
- b) Runtime error
- c) Undefined behavior
- d) Automatically resized
- How to initialize an integer array with values 1, 2, 3, 4?
- a) int arr[] = {1, 2, 3, 4};
- b) int arr[4] = (1, 2, 3, 4);
- c) int arr = {1, 2, 3, 4};
- d) int arr[4] = {1; 2; 3; 4};
- What is the output of:
int arr[3] = {10, 20, 30};
cout << arr[1];
- a) 10
- b) 20
- c) 30
- d) Garbage value
- Which of the following correctly declares a 2D array of 3 rows and 4 columns?
- a) int arr[3][4];
- b) int arr[4][3];
- c) int arr(3,4);
- d) int arr{3,4};
- How do you find the size of an array
arr
declared as int arr[10];
?
- a) sizeof(arr)
- b) sizeof(arr) / sizeof(int)
- c) sizeof(arr) / sizeof(arr[0])
- d) Both b and c
- Which operator is used to access elements in an array?
- Arrays in C++ are stored in:
- a) Heap
- b) Stack
- c) Data segment
- d) None of the above
- What is the default value of elements in an uninitialized local array?
- a) 0
- b) Garbage values
- c) Null
- d) -1
- Can you change the size of a static array once it is declared?
- Which header file is required to use
std::array
in C++?
- a) <array>
- b) <vector>
- c) <list>
- d) <iostream>
- How do you declare an array of strings in C++?
- a) string arr[5];
- b) char arr[5];
- c) string arr();
- d) char arr[5][20];
- What is the output of this code?
int arr[] = {1, 2, 3};
cout << sizeof(arr) / sizeof(arr[0]);
- a) 3
- b) 4
- c) 1
- d) Garbage value
- What will happen if you try to access an array element at negative index?
- a) Compile error
- b) Runtime error
- c) Undefined behavior
- d) Returns first element
- Which of the following is true about array elements in C++?
- a) They are stored in contiguous memory locations
- b) They can be of different data types
- c) Size can be changed at runtime
- d) Indexing starts from 1
- Which function can be used to copy one array to another?
- a) strcpy()
- b) memcpy()
- c) memmove()
- d) All of the above (depending on context)
- Which of these is NOT a valid way to initialize an array?
- a) int arr[3] = {1, 2, 3};
- b) int arr[] = {1, 2, 3};
- c) int arr[3] = (1, 2, 3);
- d) int arr[3]; arr[0] = 1;
- What is the value of arr[5] if the array is declared as
int arr[5] = {0};
and no other assignment is made?
- a) 0
- b) Garbage value
- c) 5
- d) Compile error
- What is the size (in bytes) of an array
int arr[10];
if int is 4 bytes?
- Can an array be passed to a function in C++?
- a) Yes, as a pointer
- b) No
- c) Yes, but only by value
- d) Yes, but only if global
- What is the syntax to declare a constant array in C++?
- a) const int arr[5];
- b) int const arr[5];
- c) Both a and b
- d) None
- What happens if you use `delete` on a statically declared array?
- a) Compile error
- b) Runtime error
- c) Undefined behavior
- d) Deletes array properly
- Which of these is a dynamic array in C++?
- a) int arr[10];
- b) int* arr = new int[10];
- c) vector arr(10);
- d) Both b and c
- What will `arr[0]` contain if you declare `int arr[5];` but do not initialize it?
- a) 0
- b) Garbage value
- c) Null
- d) Compile error
- How to access the last element of an array `arr` of size `n`?
- a) arr[n]
- b) arr[n-1]
- c) arr[last]
- d) arr[-1]
- Is this valid: `int arr[] = {1, 2, 3, 4, 5}; arr[5] = 6;`?
- a) Yes
- b) No
- c) Depends on compiler
- d) It overwrites the 6th element
- Which of these containers provides dynamic resizing and is safer than raw arrays?
- a) vector
- b) list
- c) array
- d) map
- Which header is needed for using `std::vector`?
- a) <vector>
- b) <array>
- c) <list>
- d) <iostream>
- What is the base address of an array?
- a) Address of first element
- b) Address of last element
- c) Null pointer
- d) Undefined
- What is the output of this code snippet?
int arr[3] = {1, 2, 3};
cout << *(arr + 2);
- a) 1
- b) 2
- c) 3
- d) Garbage value
- How do you declare a pointer to an array?
- a) int* ptr;
- b) int (*ptr)[5];
- c) int ptr[];
- d) int &ptr;
- Which of the following is NOT valid syntax to initialize an array of 5 integers with zero?
- a) int arr[5] = {};
- b) int arr[5] = {0};
- c) int arr[5] = {0, 0, 0, 0, 0};
- d) int arr[5] = (0);
- What is the difference between an array and a pointer in C++?
- a) Array is fixed size, pointer can be reassigned
- b) Pointer stores address, array stores values
- c) Pointer can do arithmetic, array cannot
- d) No difference
- What is the output of this code?
int arr[3] = {1, 2, 3};
cout << arr;
- a) 1
- b) Address of first element
- c) 0
- d) Compile error
- Can you return an array from a function in C++?
- a) Yes
- b) No
- c) Yes, using pointers or references
- d) Only if static
- Which of the following represents a jagged array?
- a) int arr[3][4];
- b) int* arr[3];
- c) vector<vector<int>>
- d) Both b and c
- Which STL container mimics a dynamic array?
- a) array
- b) vector
- c) list
- d) map
- What is the output of this?
int arr[5] = {1};
for(int i = 0; i < 5; i++)
cout << arr[i] << " ";
- a) 1 0 0 0 0
- b) 1 1 1 1 1
- c) Garbage values
- d) Compile error
- What will happen if you try to access arr[10] when array size is 10?
- a) Compiler error
- b) Runtime error
- c) Undefined behavior
- d) Returns 0
- How do you declare a multidimensional array of 2x3?
- a) int arr[2][3];
- b) int arr[3][2];
- c) int arr(2,3);
- d) int arr{2,3};
- Which of these is true about array size in C++?
- a) It must be a compile-time constant for static arrays
- b) It can be a variable in C++11
- c) Arrays can be resized at runtime
- d) None of the above
- Which of the following is the correct way to iterate over an array named `arr` with `n` elements?
- a) for(int i=0; i<n; i++)
- b) for(int i=1; i<=n; i++)
- c) for(int i=0; i<=n; i++)
- d) for(int i=1; i<n; i++)
- Which function helps in sorting an array in C++?
- a) sort()
- b) qsort()
- c) both a and b
- d) None
- What is the output of:
int arr[5] = {1,2,3,4,5};
cout << *(arr + 3);
- a) 3
- b) 4
- c) 5
- d) Garbage
- Can arrays be initialized partially in C++?
- a) Yes, remaining elements are zero-initialized
- b) No
- c) Yes, remaining elements contain garbage
- d) Depends on compiler
- How can you pass a 2D array to a function?
- a) By pointer with known column size
- b) By pointer with unknown size
- c) Only by reference
- d) Not possible
- What does this declaration mean?
int arr[5] = {1, 2};
- a) arr[0] = 1, arr[1] = 2, others zero
- b) arr[0] = 1, arr[1] = 2, others garbage
- c) Compile error
- d) All elements are 1
- Is this valid syntax? `int arr[];`
- Which of the following is true about arrays and pointers?
- a) The name of the array is a constant pointer
- b) Arrays can be assigned to pointers
- c) Pointer arithmetic is not allowed on arrays
- d) Arrays and pointers are the same
- Which operator is used to get the address of an array?
- What is the output of the code below?
int arr[3] = {1, 2, 3};
cout << arr + 1;
- a) Address of arr[1]
- b) Value 2
- c) Compile error
- d) Garbage value
- How many bytes does a `char arr[10];` take if `char` is 1 byte?
- a) 10
- b) 20
- c) 1
- d) Depends on compiler
- Can arrays be initialized at runtime in C++?
- a) No, static arrays must be initialized at compile time
- b) Yes, with dynamic allocation
- c) Yes, using vectors
- d) Both b and c
- Which of these will cause a segmentation fault?
- a) Accessing arr[100] when size is 10
- b) Accessing arr[-1]
- c) Accessing arr[0] uninitialized
- d) Both a and b
- What does the following do? `int* ptr = arr;`
- a) Points to first element of array
- b) Copies array contents to ptr
- c) Invalid assignment
- d) None
- How do you find the number of elements in an array `arr` of type `int`?
- a) sizeof(arr) / sizeof(int)
- b) sizeof(arr) / sizeof(arr[0])
- c) Length property
- d) Both a and b
- Which of the following will NOT compile?
- a) int arr[5] = {1,2,3,4,5};
- b) int arr[5] = {1,2,3,4,5,6};
- c) int arr[5] = {1};
- d) int arr[] = {1,2,3};
- What is the default value of static array elements in C++?
- a) Garbage
- b) 0
- c) Undefined
- d) Depends on compiler
- Which of the following is true about multi-dimensional arrays?
- a) They are arrays of arrays
- b) Memory is contiguous
- c) The name represents the address of the first element
- d) All of the above
0 Comments