Unit 2 - Practice Quiz

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

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

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

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

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

A. void
B. nil
C. NULL
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. Friend Function
C. Static Constructor
D. Copy Constructor

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

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

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

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

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

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

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

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() is for C-strings, size() is for objects
B. length() returns bytes, size() returns characters
C. There is no difference; they are synonyms
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. convert()
B. get_string()
C. to_char()
D. c_str()

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

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

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

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

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 rows
B. The size of each element
C. The total number of elements
D. The number of columns

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

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. 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. The string changes successfully
B. Compiler error always
C. Undefined behavior (often a segmentation fault)
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 4 bytes
B. Compiler Error or Non-standard extension
C. Resets pointer to NULL
D. Advances by 1 byte

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

31 Can a reference variable accept a NULL value?

A. Yes
B. No
C. Only static references
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 x
C. The value of p
D. The address of p

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

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

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

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

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

A. sub()
B. replace()
C. swap()
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. Only if the members are static
C. Only if the members are mutable
D. No, never

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

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

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

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

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 cannot read characters
C. It crashes on capital letters
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 moves the data
B. It copies the content (deep copy)
C. It results in an error
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. part()
B. cut()
C. slice()
D. substr()

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

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

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