1Which of the following best describes a void pointer in C++?
A.A pointer that can point to any data type
B.A pointer that is automatically initialized to NULL
C.A pointer that points to a function with void return type
D.A pointer that points to a specific data type but has no value
Correct Answer: A pointer that can point to any data type
Explanation:
A void pointer is a generic pointer that can hold the address of any data type, but it cannot be dereferenced directly without casting.
Incorrect! Try again.
2If 'ptr' is an integer pointer holding the address 1000, and the size of an integer is 4 bytes, what will be the address after executing 'ptr + 1'?
A.1004
B.1000
C.1002
D.1001
Correct Answer: 1004
Explanation:
Pointer arithmetic advances the pointer by the size of the data type it points to. 1000 + 1 * sizeof(int) = 1004.
Incorrect! Try again.
3Which operator is used to access the value pointed to by a pointer?
A..
B.*
C.->
D.&
Correct Answer: *
Explanation:
The dereference operator (*) is used to access the value stored at the memory address held by the pointer.
Incorrect! Try again.
4What is a pointer to a pointer?
A.A pointer holding the value of a variable
B.A pointer holding the address of another pointer
C.A reference variable
D.A constant pointer
Correct Answer: A pointer holding the address of another pointer
Explanation:
A pointer to a pointer stores the memory address of another pointer variable. It is declared using double asterisks (e.g., int **ptr).
Incorrect! Try again.
5Which scenario results in a 'Dangling Pointer'?
A.When a void pointer is used
B.When a pointer is assigned NULL
C.When a pointer points to a memory location that has been deleted or freed
D.When a pointer is declared but not initialized
Correct Answer: When a pointer points to a memory location that has been deleted or freed
Explanation:
A dangling pointer arises when an object is deleted or deallocated, without modifying the value of the pointer, so the pointer still points to the memory location of the deallocated memory.
Incorrect! Try again.
6What is a 'Wild Pointer'?
A.A pointer declared but not initialized
B.A pointer pointing to the base class
C.A pointer pointing to a valid object
D.A pointer set to nullptr
Correct Answer: A pointer declared but not initialized
Explanation:
Uninitialized pointers are known as wild pointers because they point to some arbitrary memory location.
Incorrect! Try again.
7Which keyword is used in C++11 and later to represent a null pointer safely?
A.void
B.nil
C.NULL
D.nullptr
Correct Answer: nullptr
Explanation:
C++11 introduced 'nullptr' as a type-safe null pointer constant, distinct from the integer 0 or NULL macro.
Incorrect! Try again.
8When a class contains a pointer member, what specific member function is often required to prevent shallow copy issues?
A.Default Constructor
B.Friend Function
C.Static Constructor
D.Copy Constructor
Correct Answer: Copy Constructor
Explanation:
If a class has a pointer member, the default copy constructor performs a shallow copy (copying the address). A user-defined copy constructor is needed for a deep copy to replicate the data pointed to.
Incorrect! Try again.
9Which operator is used to access members of a class through a pointer to an object?
A.::
B.->
C.&
D..
Correct Answer: ->
Explanation:
The arrow operator (->) is used to access class members when working with a pointer to the object.
Incorrect! Try again.
10The 'this' pointer is accessible:
A.In static member functions
B.Outside the class only
C.In non-static member functions
D.In friend functions
Correct Answer: In non-static member functions
Explanation:
The 'this' pointer is passed as a hidden argument to all non-static member functions and points to the object for which the member function is called.
Incorrect! Try again.
11What is the type of the 'this' pointer in a non-const member function of class X?
A.const X*
B.X* const
C.X&
D.X*
Correct Answer: X* const
Explanation:
The 'this' pointer is a constant pointer to the object. You cannot change the address stored in 'this', making it 'X* const'.
Incorrect! Try again.
12How do you declare an array of 10 objects of class 'Student'?
A.Student *arr = new Student;
B.Student arr;
C.arr<Student> 10;
D.Student arr[10];
Correct Answer: Student arr[10];
Explanation:
The syntax 'ClassName arrayName[size];' is used to statically declare an array of objects.
Incorrect! Try again.
13When declaring an array of objects, which constructor is invoked for each element?
A.Default constructor
B.Copy constructor
C.Destructor
D.Parameterized constructor
Correct Answer: Default constructor
Explanation:
When an array of objects is instantiated, the default constructor is called for every object in the array unless initialization lists are used (C++11).
Incorrect! Try again.
14Which header file is required to use the Standard C++ string class?
A.<stdlib.h>
B.<string.h>
C.<string>
D.<cstring>
Correct Answer: <string>
Explanation:
The Standard C++ string class is defined in the <string> header file. <cstring> is for C-style strings.
Incorrect! Try again.
15Which operator is used to concatenate two C++ string objects?
A..
B.->
C.&
D.+
Correct Answer: +
Explanation:
The '+' operator is overloaded in the string class to perform string concatenation.
Incorrect! Try again.
16What is the difference between string::length() and string::size()?
A.length() is for C-strings, size() is for objects
D.size() includes the null terminator, length() does not
Correct Answer: There is no difference; they are synonyms
Explanation:
In the standard C++ string class, both length() and size() return the same value (the number of characters). size() is provided for consistency with STL containers.
Incorrect! Try again.
17Which function is used to convert a C++ string object to a C-style string (char*)?
A.convert()
B.get_string()
C.to_char()
D.c_str()
Correct Answer: c_str()
Explanation:
The c_str() member function returns a pointer to a null-terminated character array containing the string's data.
Incorrect! Try again.
18Which string member function checks if the string is empty?
A.clear()
B.null()
C.empty()
D.void()
Correct Answer: empty()
Explanation:
The empty() function returns a boolean value: true if the string length is 0, otherwise false.
Incorrect! Try again.
19How does a reference variable differ from a pointer regarding initialization?
A.Pointers must be initialized at declaration; references do not have to be
B.Both must be initialized at declaration
C.Neither requires initialization at declaration
D.References must be initialized at declaration; pointers do not have to be
Correct Answer: References must be initialized at declaration; pointers do not have to be
Explanation:
A reference is an alias and must refer to an existing object immediately upon creation. It cannot be uninitialized.
Incorrect! Try again.
20Can a reference variable be reassigned to refer to a different variable after initialization?
A.Yes, anytime
B.Yes, but only once
C.Yes, if casted
D.No, it cannot be reassigned
Correct Answer: No, it cannot be reassigned
Explanation:
Once a reference is bound to a variable, it cannot be changed to refer to another variable. Assignments to the reference change the value of the bound variable.
Incorrect! Try again.
21Which of the following is valid syntax for declaring a reference to an integer 'a'?
A.int &r = a;
B.int r = &a;
C.int *r = a;
D.int &r = &a;
Correct Answer: int &r = a;
Explanation:
The ampersand (&) after the type indicates a reference declaration. It is initialized with the variable 'a' itself, not its address.
Incorrect! Try again.
22In a 2D array declaration 'int arr[3][4]', what does '3' represent?
A.The number of rows
B.The size of each element
C.The total number of elements
D.The number of columns
Correct Answer: The number of rows
Explanation:
In C++, the first dimension represents the number of rows, and the second dimension represents the number of columns.
Incorrect! Try again.
23How are elements of a multidimensional array stored in memory in C++?
A.Linked list format
B.Random order
C.Row-major order
D.Column-major order
Correct Answer: Row-major order
Explanation:
C++ stores multidimensional arrays in row-major order, meaning elements of the first row are stored contiguously, followed by the second row, and so on.
Incorrect! Try again.
24Which is the correct syntax to define a pointer to a data member of class 'Test' of type int?
A.int *Test::ptr;
B.int Test::*ptr;
C.int &Test::ptr;
D.Test::int *ptr;
Correct Answer: int Test::*ptr;
Explanation:
The syntax 'Type ClassName::*PointerName' is used to declare a pointer to a data member.
Incorrect! Try again.
25Which operator is used to access a data member using a pointer to data member and an object instance?
A.->*
B..*
C..
D.::
Correct Answer: .*
Explanation:
The '.' operator is used when you have an object instance and a pointer to a member. (The '->' is used if you have a pointer to the object).
Incorrect! Try again.
26If string s = "Hello";, what does s.at(1) return?
A.o
B.l
C.H
D.e
Correct Answer: e
Explanation:
String indices are 0-based. Index 0 is 'H', and index 1 is 'e'.
Incorrect! Try again.
27What happens if you try to modify a string literal assigned to a char* in C++? (e.g., char* p = "Hello"; p[0] = 'X';)
A.The string changes successfully
B.Compiler error always
C.Undefined behavior (often a segmentation fault)
D.The pointer becomes null
Correct Answer: Undefined behavior (often a segmentation fault)
Explanation:
String literals are stored in read-only memory. Attempting to modify them via a pointer results in undefined behavior.
Incorrect! Try again.
28Which modifier function appends a string to the end of the current string object?
A.push_back()
B.insert()
C.append()
D.attach()
Correct Answer: append()
Explanation:
The append() member function is specifically designed to add characters or strings to the end of the current string.
Incorrect! Try again.
29What does the following pointer arithmetic expression result in? void *p; p++;
A.Advances by 4 bytes
B.Compiler Error or Non-standard extension
C.Resets pointer to NULL
D.Advances by 1 byte
Correct Answer: Compiler Error or Non-standard extension
Explanation:
Arithmetic on void pointers is not allowed in standard C++ because the size of the pointed-to type is unknown. (GCC allows it as an extension, treating size as 1, but it is not standard).
Incorrect! Try again.
30When passing a multidimensional array to a function, which dimension must be specified?
A.Only the first dimension
B.No dimensions are needed
C.All dimensions except the first
D.All dimensions except the last
Correct Answer: All dimensions except the first
Explanation:
When passing an array to a function, the compiler needs to know the size of the inner dimensions to calculate memory offsets. Therefore, all dimensions except the first must be specified.
Incorrect! Try again.
31Can a reference variable accept a NULL value?
A.Yes
B.No
C.Only static references
D.Only const references
Correct Answer: No
Explanation:
References imply an existing object. There is no such thing as a null reference in valid C++.
Incorrect! Try again.
32Given int x = 10; int *p = &x; int **q = &p;, what does **q access?
A.The address of x
B.The value of x
C.The value of p
D.The address of p
Correct Answer: The value of x
Explanation:
q holds the address of p. *q gives the value of p (which is the address of x). **q gives the value at the address of x, which is 10.
Incorrect! Try again.
33Which statement correctly deletes an array of objects allocated with new?
A.delete[] array;
B.free(array);
C.remove array;
D.delete array;
Correct Answer: delete[] array;
Explanation:
When memory is allocated using new[], it must be freed using delete[] to ensure destructors are called for all elements.
Incorrect! Try again.
34What is the purpose of the getline(cin, str) function?
A.To print a line of text
B.To read a single character
C.To read a string stopping at whitespace
D.To read a line of text including spaces
Correct Answer: To read a line of text including spaces
Explanation:
cin >> str stops at whitespace. getline(cin, str) reads input until a newline character is found, allowing strings with spaces.
Incorrect! Try again.
35In string::npos, what does npos usually represent?
A.A null pointer
B.Zero
C.The maximum possible value for size_t, indicating 'not found'
D.The start of the string
Correct Answer: The maximum possible value for size_t, indicating 'not found'
Explanation:
npos is a static member constant value with the greatest possible value for an element of type size_t, returned by functions like find when no match is found.
Incorrect! Try again.
36Which string function replaces a part of the string with another string?
A.sub()
B.replace()
C.swap()
D.exchange()
Correct Answer: replace()
Explanation:
The replace() function is used to replace a portion of the string that begins at a specified character position.
Incorrect! Try again.
37If a class has a constant member function, can it modify data members pointed to by the this pointer?
A.Yes, always
B.Only if the members are static
C.Only if the members are mutable
D.No, never
Correct Answer: Only if the members are mutable
Explanation:
In a const member function, this is a pointer to const. Regular members cannot be modified unless they are declared with the mutable keyword.
Incorrect! Try again.
38What is the output of comparing two string objects using ==?
The string class overloads the == operator to compare the actual character content of the strings, not their addresses.
Incorrect! Try again.
39Which allows a pointer to be modified but the data it points to cannot be changed?
A.const int * const ptr
B.int * const ptr
C.int & ptr
D.const int * ptr
Correct Answer: const int * ptr
Explanation:
const int * ptr (or int const * ptr) is a pointer to a constant integer. You can change the pointer to point elsewhere, but you cannot change the integer value via the pointer.
Incorrect! Try again.
40What does int * const ptr define?
A.A pointer to a constant integer
B.A reference to an integer
C.A constant pointer to a constant integer
D.A constant pointer to an integer
Correct Answer: A constant pointer to an integer
Explanation:
int * const ptr means the pointer itself is constant (address cannot change), but the value it points to can be modified.
Incorrect! Try again.
41Which operator allows accessing members of an object using a pointer to a member function (func_ptr) and a pointer to an object (obj_ptr)?
A.(obj_ptr->*func_ptr)()
B.obj_ptr.*func_ptr()
C.obj_ptr->func_ptr()
D.(obj_ptr.*func_ptr)()
Correct Answer: (obj_ptr->*func_ptr)()
Explanation:
The ->* operator is used to access a member function via a pointer to that member, using a pointer to the object.
Incorrect! Try again.
42Why is string s; cin >> s; potentially problematic for reading names like "New York"?
A.It only reads "New" and leaves "York" in the buffer
B.It cannot read characters
C.It crashes on capital letters
D.It requires a char array
Correct Answer: It only reads "New" and leaves "York" in the buffer
Explanation:
The extraction operator >> stops reading at the first whitespace. getline should be used for inputs with spaces.
Incorrect! Try again.
43How do you access the element at row 1, column 2 of a 2D array named matrix?
A.matrix[1,2]
B.matrix{1}{2}
C.matrix(1,2)
D.matrix[1][2]
Correct Answer: matrix[1][2]
Explanation:
C++ uses multiple bracket sets [][] for accessing elements in multidimensional arrays.
Incorrect! Try again.
44What happens when you assign one C++ string object to another (e.g., str1 = str2)?
A.It moves the data
B.It copies the content (deep copy)
C.It results in an error
D.It copies the address (shallow copy)
Correct Answer: It copies the content (deep copy)
Explanation:
The standard string class handles memory management internally and performs a deep copy of the character data upon assignment.
Incorrect! Try again.
45Can an array be a data member of a class?
A.Yes
B.Only if it is const
C.No, only pointers
D.Only if it is static
Correct Answer: Yes
Explanation:
Classes can contain arrays as data members. The size must be known at compile time unless dynamic allocation is used.
Incorrect! Try again.
46Which string method returns a substring?
A.part()
B.cut()
C.slice()
D.substr()
Correct Answer: substr()
Explanation:
substr(pos, len) returns a string object containing a substring of length len starting at pos.
Incorrect! Try again.
47If p is a null pointer, what is the result of delete p;?
A.Nothing happens (it is safe)
B.Compiler error
C.Runtime crash
D.Memory leak
Correct Answer: Nothing happens (it is safe)
Explanation:
The C++ standard specifies that delete on a null pointer effectively does nothing and is safe to execute.
Incorrect! Try again.
48What is the specific issue called when delete is forgotten for memory allocated with new?
A.Segmentation fault
B.Wild pointer
C.Memory leak
D.Dangling pointer
Correct Answer: Memory leak
Explanation:
A memory leak occurs when dynamically allocated memory is not released back to the system, reducing available memory.
Incorrect! Try again.
49Which of the following correctly declares a pointer to a function that takes an int and returns void?
A.*void func(int);
B.void func(int);
C.void (*func)(int);
D.void *func(int);
Correct Answer: void (*func)(int);
Explanation:
The syntax ReturnType (*PointerName)(ParameterTypes) is used for function pointers. The parentheses around *func are necessary.
Incorrect! Try again.
50In pointer arithmetic, subtracting two pointers of the same type results in:
A.The number of elements between them
B.The number of bytes between them
C.Compiler error
D.The sum of their addresses
Correct Answer: The number of elements between them
Explanation:
Subtracting two pointers returns the number of elements (of the pointed-to type) separating them in memory, not the raw byte difference.