Unit 2 - Practice Quiz

CSE202

1 Which 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

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. 1004
C. 1000
D. 1002

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 pointer holding the address of another pointer
C. A reference variable
D. A constant pointer

5 Which 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

6 What 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

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

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

8 When 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

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 static member functions
B. In non-static member functions
C. Outside the class only
D. In friend functions

11 What 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&

12 How 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;

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

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

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

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

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. 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

17 Which 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()

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

A. clear()
B. void()
C. empty()
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. 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

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

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

23 How 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

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. int Test::*ptr;
C. int &Test::ptr;
D. Test::int *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. H
B. e
C. l
D. o

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. The string changes successfully
B. Undefined behavior (often a segmentation fault)
C. Compiler error always
D. The pointer becomes null

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

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

29 What 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

30 When 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

31 Can a reference variable accept a NULL value?

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

32 Given 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

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

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

34 What 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

35 In 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

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

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

37 If 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

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

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

39 Which 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

40 What 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

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

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 copies the address (shallow copy)
B. It copies the content (deep copy)
C. It results in an error
D. It moves the data

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

46 Which string method returns a substring?

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

47 If 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

48 What 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

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. The number of elements between them
C. The number of bytes between them
D. Compiler error