Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 2 MCQsPointers, Reference Variables, Arrays and String Concepts
Which of the following best describes a void pointer in C++?
Not quite. Try another option.
Answer: A pointer that can point to any data type
A void pointer is a generic pointer that can hold the address of any data type, but it cannot be dereferenced directly without casting.
Question 2 of 50
If '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'?
Not quite. Try another option.
Answer: 1004
Pointer arithmetic advances the pointer by the size of the data type it points to. 1000 + 1 * sizeof(int) = 1004.
Question 3 of 50
Which operator is used to access the value pointed to by a pointer?
Not quite. Try another option.
Answer: *
The dereference operator (*) is used to access the value stored at the memory address held by the pointer.
Question 4 of 50
What is a pointer to a pointer?
Not quite. Try another option.
Answer: A pointer holding the address of another pointer
A pointer to a pointer stores the memory address of another pointer variable. It is declared using double asterisks (e.g., int **ptr).
Question 5 of 50
Which scenario results in a 'Dangling Pointer'?
Not quite. Try another option.
Answer: When a pointer points to a memory location that has been deleted or freed
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.
Question 6 of 50
What is a 'Wild Pointer'?
Not quite. Try another option.
Answer: A pointer declared but not initialized
Uninitialized pointers are known as wild pointers because they point to some arbitrary memory location.
Question 7 of 50
Which keyword is used in C++11 and later to represent a null pointer safely?
Not quite. Try another option.
Answer: nullptr
C++11 introduced 'nullptr' as a type-safe null pointer constant, distinct from the integer 0 or NULL macro.
Question 8 of 50
When a class contains a pointer member, what specific member function is often required to prevent shallow copy issues?
Not quite. Try another option.
Answer: Copy Constructor
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.
Question 9 of 50
Which operator is used to access members of a class through a pointer to an object?
Not quite. Try another option.
Answer: ->
The arrow operator (->) is used to access class members when working with a pointer to the object.
Question 10 of 50
The 'this' pointer is accessible:
Not quite. Try another option.
Answer: In non-static member functions
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.
Question 11 of 50
What is the type of the 'this' pointer in a non-const member function of class X?
Not quite. Try another option.
Answer: X* const
The 'this' pointer is a constant pointer to the object. You cannot change the address stored in 'this', making it 'X* const'.
Question 12 of 50
How do you declare an array of 10 objects of class 'Student'?
Not quite. Try another option.
Answer: Student arr[10];
The syntax 'ClassName arrayName[size];' is used to statically declare an array of objects.
Question 13 of 50
When declaring an array of objects, which constructor is invoked for each element?
Not quite. Try another option.
Answer: Default constructor
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).
Question 14 of 50
Which header file is required to use the Standard C++ string class?
Not quite. Try another option.
Answer: <string>
The Standard C++ string class is defined in the <string> header file. <cstring> is for C-style strings.
Question 15 of 50
Which operator is used to concatenate two C++ string objects?
Not quite. Try another option.
Answer: +
The '+' operator is overloaded in the string class to perform string concatenation.
Question 16 of 50
What is the difference between string::length() and string::size()?
Not quite. Try another option.
Answer: There is no difference; they are synonyms
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.
Question 17 of 50
Which function is used to convert a C++ string object to a C-style string (char*)?
Not quite. Try another option.
Answer: c_str()
The c_str() member function returns a pointer to a null-terminated character array containing the string's data.
Question 18 of 50
Which string member function checks if the string is empty?
Not quite. Try another option.
Answer: empty()
The empty() function returns a boolean value: true if the string length is 0, otherwise false.
Question 19 of 50
How does a reference variable differ from a pointer regarding initialization?
Not quite. Try another option.
Answer: References must be initialized at declaration; pointers do not have to be
A reference is an alias and must refer to an existing object immediately upon creation. It cannot be uninitialized.
Question 20 of 50
Can a reference variable be reassigned to refer to a different variable after initialization?
Not quite. Try another option.
Answer: No, it cannot be reassigned
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.
Question 21 of 50
Which of the following is valid syntax for declaring a reference to an integer 'a'?
Not quite. Try another option.
Answer: int &r = a;
The ampersand (&) after the type indicates a reference declaration. It is initialized with the variable 'a' itself, not its address.
Question 22 of 50
In a 2D array declaration 'int arr[3][4]', what does '3' represent?
Not quite. Try another option.
Answer: The number of rows
In C++, the first dimension represents the number of rows, and the second dimension represents the number of columns.
Question 23 of 50
How are elements of a multidimensional array stored in memory in C++?
Not quite. Try another option.
Answer: Row-major order
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.
Question 24 of 50
Which is the correct syntax to define a pointer to a data member of class 'Test' of type int?
Not quite. Try another option.
Answer: int Test::*ptr;
The syntax 'Type ClassName::*PointerName' is used to declare a pointer to a data member.
Question 25 of 50
Which operator is used to access a data member using a pointer to data member and an object instance?
Not quite. Try another option.
Answer: .*
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).
Question 26 of 50
If string s = "Hello";, what does s.at(1) return?
Not quite. Try another option.
Answer: e
String indices are 0-based. Index 0 is 'H', and index 1 is 'e'.
Question 27 of 50
What happens if you try to modify a string literal assigned to a char* in C++? (e.g., char* p = "Hello"; p[0] = 'X';)
Not quite. Try another option.
Answer: Undefined behavior (often a segmentation fault)
String literals are stored in read-only memory. Attempting to modify them via a pointer results in undefined behavior.
Question 28 of 50
Which modifier function appends a string to the end of the current string object?
Not quite. Try another option.
Answer: append()
The append() member function is specifically designed to add characters or strings to the end of the current string.
Question 29 of 50
What does the following pointer arithmetic expression result in? void *p; p++;
Not quite. Try another option.
Answer: Compiler Error or Non-standard extension
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).
Question 30 of 50
When passing a multidimensional array to a function, which dimension must be specified?
Not quite. Try another option.
Answer: All dimensions except the first
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.
Question 31 of 50
Can a reference variable accept a NULL value?
Not quite. Try another option.
Answer: No
References imply an existing object. There is no such thing as a null reference in valid C++.
Question 32 of 50
Given int x = 10; int *p = &x; int **q = &p;, what does **q access?
Not quite. Try another option.
Answer: The value of x
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.
Question 33 of 50
Which statement correctly deletes an array of objects allocated with new?
Not quite. Try another option.
Answer: delete[] array;
When memory is allocated using new[], it must be freed using delete[] to ensure destructors are called for all elements.
Question 34 of 50
What is the purpose of the getline(cin, str) function?
Not quite. Try another option.
Answer: To read a line of text including spaces
cin >> str stops at whitespace. getline(cin, str) reads input until a newline character is found, allowing strings with spaces.
Question 35 of 50
In string::npos, what does npos usually represent?
Not quite. Try another option.
Answer: The maximum possible value for size_t, indicating 'not found'
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.
Question 36 of 50
Which string function replaces a part of the string with another string?
Not quite. Try another option.
Answer: replace()
The replace() function is used to replace a portion of the string that begins at a specified character position.
Question 37 of 50
If a class has a constant member function, can it modify data members pointed to by the this pointer?
Not quite. Try another option.
Answer: Only if the members are mutable
In a const member function, this is a pointer to const. Regular members cannot be modified unless they are declared with the mutable keyword.
Question 38 of 50
What is the output of comparing two string objects using ==?
Not quite. Try another option.
Answer: Compares content lexicographically
The string class overloads the == operator to compare the actual character content of the strings, not their addresses.
Question 39 of 50
Which allows a pointer to be modified but the data it points to cannot be changed?
Not quite. Try another option.
Answer: const int * ptr
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.
Question 40 of 50
What does int * const ptr define?
Not quite. Try another option.
Answer: A constant pointer to an integer
int * const ptr means the pointer itself is constant (address cannot change), but the value it points to can be modified.
Question 41 of 50
Which operator allows accessing members of an object using a pointer to a member function (func_ptr) and a pointer to an object (obj_ptr)?
Not quite. Try another option.
Answer: (obj_ptr->*func_ptr)()
The ->* operator is used to access a member function via a pointer to that member, using a pointer to the object.
Question 42 of 50
Why is string s; cin >> s; potentially problematic for reading names like "New York"?
Not quite. Try another option.
Answer: It only reads "New" and leaves "York" in the buffer
The extraction operator >> stops reading at the first whitespace. getline should be used for inputs with spaces.
Question 43 of 50
How do you access the element at row 1, column 2 of a 2D array named matrix?
Not quite. Try another option.
Answer: matrix[1][2]
C++ uses multiple bracket sets [][] for accessing elements in multidimensional arrays.
Question 44 of 50
What happens when you assign one C++ string object to another (e.g., str1 = str2)?
Not quite. Try another option.
Answer: It copies the content (deep copy)
The standard string class handles memory management internally and performs a deep copy of the character data upon assignment.
Question 45 of 50
Can an array be a data member of a class?
Not quite. Try another option.
Answer: Yes
Classes can contain arrays as data members. The size must be known at compile time unless dynamic allocation is used.
Question 46 of 50
Which string method returns a substring?
Not quite. Try another option.
Answer: substr()
substr(pos, len) returns a string object containing a substring of length len starting at pos.
Question 47 of 50
If p is a null pointer, what is the result of delete p;?
Not quite. Try another option.
Answer: Nothing happens (it is safe)
The C++ standard specifies that delete on a null pointer effectively does nothing and is safe to execute.
Question 48 of 50
What is the specific issue called when delete is forgotten for memory allocated with new?
Not quite. Try another option.
Answer: Memory leak
A memory leak occurs when dynamically allocated memory is not released back to the system, reducing available memory.
Question 49 of 50
Which of the following correctly declares a pointer to a function that takes an int and returns void?
Not quite. Try another option.
Answer: void (*func)(int);
The syntax ReturnType (*PointerName)(ParameterTypes) is used for function pointers. The parentheses around *func are necessary.
Question 50 of 50
In pointer arithmetic, subtracting two pointers of the same type results in:
Not quite. Try another option.
Answer: The number of elements between them
Subtracting two pointers returns the number of elements (of the pointed-to type) separating them in memory, not the raw byte difference.
That's unit 2
Answer a few questions and your score shows up here.
Nearly through. Did unit 2 of CSE202 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 2 of CSE202 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.