1Which of the following best describes a void pointer in C++?
A.A pointer that points to a specific data type but has no value
B.A pointer that can point to any data type
C.A pointer that points to a function with void return type
D.A pointer that is automatically initialized to NULL
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.1001
B.1004
C.1000
D.1002
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 pointer is assigned NULL
B.When a pointer is declared but not initialized
C.When a pointer points to a memory location that has been deleted or freed
D.When a void pointer is used
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 pointing to a valid object
B.A pointer declared but not initialized
C.A pointer set to nullptr
D.A pointer pointing to the base class
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.NULL
B.nil
C.nullptr
D.void
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.Copy Constructor
C.Static Constructor
D.Friend Function
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.In non-static member functions
C.Outside the class only
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.X*
B.const X*
C.X* const
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;
B.Student arr[10];
C.Student *arr = new Student;
D.arr<Student> 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.Copy constructor
B.Parameterized constructor
C.Default constructor
D.Destructor
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.<string.h>
B.<cstring>
C.<string>
D.<stdlib.h>
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()?
B.size() includes the null terminator, length() does not
C.There is no difference; they are synonyms
D.length() is for C-strings, size() is for objects
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.to_char()
B.c_str()
C.get_string()
D.convert()
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.void()
C.empty()
D.null()
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.References must be initialized at declaration; pointers do not have to be
B.Pointers must be initialized at declaration; references do not have to be
C.Both must be initialized at declaration
D.Neither requires initialization at declaration
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.No, it cannot be reassigned
D.Yes, if casted
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 columns
B.The total number of elements
C.The number of rows
D.The size of each element
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.Row-major order
B.Column-major order
C.Random order
D.Linked list format
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.H
B.e
C.l
D.o
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.Undefined behavior (often a segmentation fault)
C.Compiler error always
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 1 byte
B.Advances by 4 bytes
C.Compiler Error or Non-standard extension
D.Resets pointer to NULL
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.All dimensions except the first
C.All dimensions except the last
D.No dimensions are needed
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 const references
D.Only static 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 p
B.The address of x
C.The value of x
D.The value 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.delete[] array;
C.remove array;
D.free(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 read a single character
B.To read a string stopping at whitespace
C.To read a line of text including spaces
D.To print a line of text
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.The start of the string
B.The maximum possible value for size_t, indicating 'not found'
C.A null pointer
D.Zero
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.swap()
B.sub()
C.replace()
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.No, never
C.Only if the members are mutable
D.Only if the members are static
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 ==?
Explanation: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.int * const ptr
B.const int * ptr
C.const int * const ptr
D.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 constant pointer to an integer
C.A constant pointer to a constant integer
D.A reference 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 crashes on capital letters
B.It only reads "New" and leaves "York" in the buffer
C.It cannot read characters
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 copies the address (shallow copy)
B.It copies the content (deep copy)
C.It results in an error
D.It moves the data
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.No, only pointers
B.Yes
C.Only if it is static
D.Only if it is const
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.slice()
B.cut()
C.substr()
D.part()
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.Runtime crash
B.Compiler error
C.Nothing happens (it is safe)
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.Dangling pointer
B.Memory leak
C.Wild pointer
D.Segmentation fault
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 sum of their addresses
B.The number of elements between them
C.The number of bytes between them
D.Compiler error
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.
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.