Unit 2: Pointers, Reference Variables, Arrays and String Concepts - Practice Quiz

CSE202 — Object Oriented Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which of the following best describes a void pointer in C++?

A. A pointer that points to a function with void return type
B. A pointer that points to a specific data type but has no value
C. A pointer that is automatically initialized to NULL
D. A pointer that can point to any data type

2 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'?

A. 1001
B. 1000
C. 1002
D. 1004

3 Which operator is used to access the value pointed to by a pointer?

A. &
B. ->
C. .
D. *

4 What is a pointer to a pointer?

A. A pointer holding the value of a variable
B. A constant pointer
C. A reference variable
D. A pointer holding the address of another pointer

5 Which scenario results in a 'Dangling Pointer'?

A. When a void pointer is used
B. When a pointer points to a memory location that has been deleted or freed
C. When a pointer is declared but not initialized
D. When a pointer is assigned NULL

6 What is a 'Wild Pointer'?

A. A pointer pointing to the base class
B. A pointer declared but not initialized
C. A pointer set to nullptr
D. A pointer pointing to a valid object

7 Which keyword is used in C++11 and later to represent a null pointer safely?

A. NULL
B. nil
C. void
D. nullptr

8 When a class contains a pointer member, what specific member function is often required to prevent shallow copy issues?

A. Default Constructor
B. Static Constructor
C. Copy Constructor
D. Friend Function

9 Which operator is used to access members of a class through a pointer to an object?

A. &
B. ::
C. ->
D. .

10 The 'this' pointer is accessible:

A. In friend functions
B. In non-static member functions
C. Outside the class only
D. In static member functions

11 What is the type of the 'this' pointer in a non-const member function of class X?

A. const X*
B. X&
C. X*
D. X* const

12 How do you declare an array of 10 objects of class 'Student'?

A. Student *arr = new Student;
B. arr<Student> 10;
C. Student arr[10];
D. Student arr;

13 When declaring an array of objects, which constructor is invoked for each element?

A. Copy constructor
B. Default constructor
C. Parameterized constructor
D. Destructor

14 Which header file is required to use the Standard C++ string class?

A. <string.h>
B. <stdlib.h>
C. <cstring>
D. <string>

15 Which operator is used to concatenate two C++ string objects?

A. .
B. +
C. &
D. ->

16 What is the difference between string::length() and string::size()?

A. length() returns bytes, size() returns characters
B. There is no difference; they are synonyms
C. length() is for C-strings, size() is for objects
D. size() includes the null terminator, length() does not

17 Which function is used to convert a C++ string object to a C-style string (char*)?

A. c_str()
B. get_string()
C. convert()
D. to_char()

18 Which string member function checks if the string is empty?

A. empty()
B. clear()
C. void()
D. null()

19 How does a reference variable differ from a pointer regarding initialization?

A. References must be initialized at declaration; pointers do not have to be
B. Neither requires initialization at declaration
C. Pointers must be initialized at declaration; references do not have to be
D. Both must be initialized at declaration

20 Can a reference variable be reassigned to refer to a different variable after initialization?

A. Yes, but only once
B. Yes, if casted
C. No, it cannot be reassigned
D. Yes, anytime

21 Which 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;

22 In 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 size of each element
D. The number of rows

23 How are elements of a multidimensional array stored in memory in C++?

A. Random order
B. Linked list format
C. Column-major order
D. Row-major order

24 Which is the correct syntax to define a pointer to a data member of class 'Test' of type int?

A. int &Test::ptr;
B. Test::int *ptr;
C. int *Test::ptr;
D. int Test::*ptr;

25 Which operator is used to access a data member using a pointer to data member and an object instance?

A. .
B. ->*
C. .*
D. ::

26 If string s = "Hello";, what does s.at(1) return?

A. o
B. l
C. H
D. e

27 What happens if you try to modify a string literal assigned to a char* in C++? (e.g., char* p = "Hello"; p[0] = 'X';)

A. Undefined behavior (often a segmentation fault)
B. The pointer becomes null
C. The string changes successfully
D. Compiler error always

28 Which modifier function appends a string to the end of the current string object?

A. insert()
B. attach()
C. append()
D. push_back()

29 What does the following pointer arithmetic expression result in? void *p; p++;

A. Compiler Error or Non-standard extension
B. Advances by 4 bytes
C. Advances by 1 byte
D. Resets pointer to NULL

30 When passing a multidimensional array to a function, which dimension must be specified?

A. All dimensions except the last
B. All dimensions except the first
C. No dimensions are needed
D. Only the first dimension

31 Can a reference variable accept a NULL value?

A. Yes
B. Only static references
C. No
D. Only const references

32 Given int x = 10; int *p = &x; int **q = &p;, what does **q access?

A. The address of x
B. The value of p
C. The address of p
D. The value of x

33 Which statement correctly deletes an array of objects allocated with new?

A. delete[] array;
B. remove array;
C. free(array);
D. delete array;

34 What is the purpose of the getline(cin, str) function?

A. To read a string stopping at whitespace
B. To print a line of text
C. To read a line of text including spaces
D. To read a single character

35 In string::npos, what does npos usually represent?

A. The maximum possible value for size_t, indicating 'not found'
B. Zero
C. The start of the string
D. A null pointer

36 Which string function replaces a part of the string with another string?

A. replace()
B. exchange()
C. swap()
D. sub()

37 If a class has a constant member function, can it modify data members pointed to by the this pointer?

A. No, never
B. Only if the members are static
C. Only if the members are mutable
D. Yes, always

38 What is the output of comparing two string objects using ==?

A. Always returns true
B. Compares content lexicographically
C. Compares string lengths
D. Compares memory addresses

39 Which allows a pointer to be modified but the data it points to cannot be changed?

A. const int * const ptr
B. int & ptr
C. const int * ptr
D. int * const ptr

40 What does int * const ptr define?

A. A constant pointer to an integer
B. A constant pointer to a constant integer
C. A reference to an integer
D. A pointer to a constant integer

41 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)?

A. obj_ptr.*func_ptr()
B. (obj_ptr->*func_ptr)()
C. (obj_ptr.*func_ptr)()
D. obj_ptr->func_ptr()

42 Why 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 requires a char array
C. It crashes on capital letters
D. It cannot read characters

43 How 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]

44 What happens when you assign one C++ string object to another (e.g., str1 = str2)?

A. It results in an error
B. It copies the content (deep copy)
C. It moves the data
D. It copies the address (shallow copy)

45 Can 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

46 Which string method returns a substring?

A. cut()
B. part()
C. substr()
D. slice()

47 If p is a null pointer, what is the result of delete p;?

A. Runtime crash
B. Compiler error
C. Memory leak
D. Nothing happens (it is safe)

48 What is the specific issue called when delete is forgotten for memory allocated with new?

A. Dangling pointer
B. Segmentation fault
C. Memory leak
D. Wild pointer

49 Which 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);

50 In pointer arithmetic, subtracting two pointers of the same type results in:

A. The sum of their addresses
B. Compiler error
C. The number of elements between them
D. The number of bytes between them