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 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 can point to any data 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. 1000
C. 1004
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 reference variable
B. A constant pointer
C. A pointer holding the address of another pointer
D. A pointer holding the value of a variable

5 Which scenario results in a 'Dangling Pointer'?

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

6 What is a 'Wild Pointer'?

A. A pointer set to nullptr
B. A pointer declared but not initialized
C. A pointer pointing to a valid object
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. void
B. nullptr
C. nil
D. NULL

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

A. Friend Function
B. Static Constructor
C. Copy Constructor
D. Default 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. Outside the class only
B. In non-static member functions
C. In friend functions
D. In static member 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 = new Student;
C. arr<Student> 10;
D. Student arr[10];

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

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

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

A. <stdlib.h>
B. <cstring>
C. <string.h>
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. 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. c_str()
B. convert()
C. to_char()
D. get_string()

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

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

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

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

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

A. Yes, but only once
B. No, it cannot be reassigned
C. Yes, anytime
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 total number of elements
B. The number of rows
C. The size of each element
D. The number of columns

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

A. Random order
B. Column-major order
C. Row-major 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. 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. e
C. l
D. H

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. The pointer becomes null
D. Undefined behavior (often a segmentation fault)

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

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

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

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

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. Only static references
B. Only const references
C. Yes
D. No

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

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

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

A. remove array;
B. free(array);
C. delete 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 line of text including spaces
C. To read a single character
D. To read a string stopping at whitespace

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. sub()
B. exchange()
C. replace()
D. swap()

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

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

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

A. Compares content lexicographically
B. Always returns true
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 * ptr
B. const int * const ptr
C. int * const ptr
D. int & ptr

40 What does int * const ptr define?

A. A constant pointer to an integer
B. A reference to an integer
C. A pointer to a constant integer
D. A constant 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 crashes on capital letters
C. It requires a char array
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 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. Only if it is static
B. No, only pointers
C. Only if it is const
D. Yes

46 Which string method returns a substring?

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

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

A. Compiler error
B. Nothing happens (it is safe)
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. Dangling pointer
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 number of elements between them
B. The sum of their addresses
C. Compiler error
D. The number of bytes between them