Unit 2: Handling Pointers, Arrays and String - Practice Quiz

CAP455 — Object Oriented Programming Using C++ 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which symbol is used to declare a reference variable in C++?

Pointer vs reference variables Easy
A. #
B. *
C. %
D. &

2 Which type of pointer can store the address of an object of any data type?

Void pointer Easy
A. float*
B. int*
C. void*
D. char*

3 If p points to an element of an integer array, what does p + 1 generally indicate?

Pointer arithmetic Easy
A. The next integer
B. The previous integer
C. The first integer
D. The array size

4 Which declaration represents a pointer to a pointer to an integer?

Pointer to pointer Easy
A. int* p;
B. int p**;
C. int** p;
D. int& p;

5 What is a dangling pointer?

Dangling pointer Easy
A. A pointer with a valid object
B. A pointer to a constant
C. A pointer to deleted memory
D. A pointer to a null value

6 A pointer that has not been initialized is commonly called a:

Wild pointer Easy
A. Constant pointer
B. Null pointer
C. Smart pointer
D. Wild pointer

7 Which value is commonly used to indicate that a pointer does not point to a valid object?

Null pointer assignment Easy
A. delete
B. nullptr
C. static
D. sizeof

8 A pointer declared inside a class is called a:

Pointers as class members Easy
A. Pointer constant
B. Pointer member
C. Reference function
D. Class array

9 Which operator is used to access a member through a pointer to an object?

Pointer to objects Easy
A. ->
B. .*
C. ::
D. .

10 In a non-static member function, what does the this pointer refer to?

This pointer Easy
A. The current object
B. The main function
C. The base class
D. The next object

11 Which operator is used with an object to access a pointer to a non-static data member?

Pointer to data member Easy
A. %%
B. &&
C. ->
D. .*

12 Which declaration creates a two-dimensional integer array with 2 rows and 3 columns?

Array declaration and processing of multidimensional arrays inside main and inside class Easy
A. int a[3][2];
B. int a(2)(3);
C. int a[2,3];
D. int a[2][3];

13 Which loop structure is commonly used to process every element of a two-dimensional array?

Array declaration and processing of multidimensional arrays inside main and inside class Easy
A. Two nested loops
B. A switch statement
C. A single if statement
D. One loop only

14 What is an array of objects?

Array of objects Easy
A. A class containing pointers
B. An array containing class objects
C. A pointer containing arrays
D. A function returning objects

15 Which header file provides the standard C++ string class?

Standard C++ string class Easy
A. <pointer>
B. <string>
C. <text>
D. <array>

16 Which statement correctly defines a C++ string object containing Hello?

Defining and assigning string objects Easy
A. string s = "Hello";
B. string s = Hello;
C. String s[Hello];
D. char s = "Hello";

17 Which operator is used to assign one string object to another?

Defining and assigning string objects Easy
A. += only
B. =
C. <=
D. ==

18 Which string member function adds characters to the end of a string?

Modifiers of string class Easy
A. remove()
B. compare()
C. append()
D. lengthen()

19 Which string member function can remove characters from a string?

Modifiers of string class Easy
A. copy()
B. find()
C. erase()
D. insert()

20 Which string member function inserts characters at a specified position?

Modifiers of string class Easy
A. swap()
B. insert()
C. clear()
D. empty()

21 What is printed by the following code?

int x = 4; int y = 7; int* p = &x; int& r = y; p = &y; r = x; std::cout << *p << " " << y;

Pointer vs reference variables Medium
A. 4 4
B. 4 7
C. 7 4
D. 7 7

22 Given double value = 6.25; void* vp = &value;, which expression correctly reads the stored value?

Void pointer Medium
A. *static_cast<double*>(vp)
B. static_cast<double>(*vp)
C. *static_cast<int*>(vp)
D. static_cast<double*>(*vp)

23 What is printed by this code?

int a[] = {3, 6, 9, 12, 15}; int* p = a + 1; std::cout << *(p + 2) - *p;

Pointer arithmetic Medium
A. 6
B. 12
C. 3
D. 9

24 What is the final value of n?

int n = 5; int* p = &n; int** pp = &p; **pp += 3;

Pointer to pointer Medium
A. 5
B. 3
C. 8
D. 15

25 Which modification best prevents p from remaining a dangling pointer after the dynamically allocated object is deleted?

int* p = new int(10); delete p;

Dangling pointer Medium
A. Add p = nullptr;
B. Add p++;
C. Add delete p; again
D. Add *p = 0;

26 Which declaration creates an uninitialized pointer that may contain an indeterminate address?

Wild pointer Medium
A. int* p;
B. int* p = nullptr;
C. int x = 0; int* p = &x;
D. int* p = new int{};

27 Consider the overloads void show(int); and void show(int*);. Which call unambiguously selects the pointer overload in modern C++?

Null pointer assignment Medium
A. show(0);
B. show(NULL);
C. show(nullptr);
D. show(false);

28 A class owns an int* data allocated with new int. Why may the compiler-generated copy constructor be unsafe?

Pointers as class members Medium
A. It allocates two extra integers
B. It performs a shallow pointer copy
C. It always sets the pointer to null
D. It prevents object assignment entirely

29 Given Student s; Student* p = &s;, which statement calls the member function display() through p?

Pointer to objects Medium
A. p.display();
B. *p.display();
C. p::display();
D. p->display();

30 Which constructor body correctly assigns the parameter value to a data member that is also named value?

class Item { int value; public: Item(int value) { /* body */ } };

This pointer Medium
A. value = this;
B. this->value = value;
C. *this = value;
D. this = &value;

31 Given struct Point { int x; }; Point pt{8}; int Point::* pm = &Point::x;, which expression obtains the value 8?

Pointer to data member Medium
A. pt->*pm
B. *pt.pm
C. pm.*pt
D. pt.*pm

32 Given struct Box { int size; }; Box b{12}; Box* pb = &b; int Box::* member = &Box::size;, which expression changes size to 20?

Pointer to data member Medium
A. member->pb = 20;
B. pb.*member = 20;
C. *pb.member = 20;
D. pb->*member = 20;

33 What is printed by this code?

int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; int sum = 0; for (int i = 0; i < 2; ++i) sum += a[i][i + 1]; std::cout << sum;

Array declaration and processing of multidimensional arrays inside main and inside class Medium
A. 7
B. 6
C. 8
D. 9

34 A class contains int matrix[3][4];. Which nested-loop bounds correctly process every element exactly once?

Array declaration and processing of multidimensional arrays inside main and inside class Medium
A. i <= 2 and j < 3
B. i <= 3 and j <= 4
C. i < 4 and j < 3
D. i < 3 and j < 4

35 What is the final value of total?

class Item { public: int price; Item() : price(5) {} }; Item items[4]; int total = 0; for (const Item& x : items) total += x.price;

Array of objects Medium
A. 5
B. 20
C. 10
D. 25

36 Which declaration correctly creates three Book objects using a constructor Book(std::string, int)?

Array of objects Medium
A. Book books(3) = {{"A", 100}, {"B", 120}, {"C", 140}};
B. Book books[3] = {{"A", 100}, {"B", 120}, {"C", 140}};
C. Book[3] books = {"A", 100, "B", 120, "C", 140};
D. Book books = {{"A", 100}, {"B", 120}, {"C", 140}};

37 What is printed by the following code?

std::string s = "programming"; std::cout << s.substr(3, 4);

Standard C++ string class Medium
A. gramm
B. ogra
C. gram
D. ramm

38 What does std::string::find return when the requested substring is absent?

Standard C++ string class Medium
A. -1 as an int
B. nullptr
C. std::string::npos
D. std::string::end()

39 What is the value of s after this code executes?

std::string s(3, 'A'); std::string t = "BC"; s = s + t;

Defining and assigning string objects Medium
A. AAABC
B. ABCBC
C. AABC
D. 3ABC

40 What is the final value of s?

std::string s = "abcdef"; s.erase(2, 2); s.insert(2, "XY");

Modifiers of string class Medium
A. aXYdef
B. abcdXY
C. abXYef
D. abXYdef

41 Given int a = 10, b = 20; int* p = &a; int& r = a;, which statement sequence changes a to 20 and then makes p point to b without changing what r refers to?

Pointer vs reference variables Hard
A. r = &b; p = b;
B. p = b; r = &b;
C. r = b; p = &b;
D. p = &b; r = b;

42 What is the correct way to print the value stored in int n = 42; void* vp = &n;?

Void pointer Hard
A. std::cout << static_cast<int*>(vp);
B. std::cout << *vp;
C. std::cout << static_cast<int>(*vp);
D. std::cout << *(int*)vp;

43 For int a[5] = {2, 4, 6, 8, 10}; int* p = a + 1;, which expression evaluates to 8?

Pointer arithmetic Hard
A. *(p + 1)
B. p[3]
C. *p + 1
D. *(p - 1)

44 For int a[5] = {2, 4, 6, 8, 10}; int* p = a + 1;, which expression evaluates to 8?

Pointer arithmetic Hard
A. *(p + 1)
B. p[2]
C. *p + 1
D. *(p - 1)

45 What is printed by int x = 5; int* p = &x; int** q = &p; **q = 9; std::cout << x;?

Pointer to pointer Hard
A. The address of x
B. 9
C. 5
D. Compilation failure because q cannot be dereferenced twice

46 Which modification prevents undefined behavior in this code after the block ends? int* p; { int x = 7; p = &x; } std::cout << *p;

Dangling pointer Hard
A. Increment p before dereferencing it
B. Cast p to void* before dereferencing it
C. Declare x as static inside the block
D. Add delete p after the block

47 Which declaration creates a wild pointer whose value is indeterminate until it is initialized?

Wild pointer Hard
A. int* p = new int;
B. int* p = nullptr + 1;
C. int* p = nullptr;
D. int* p;

48 After int* p = nullptr;, which operation is well-defined?

Null pointer assignment Hard
A. std::cout << p[0];
B. std::cout << *p;
C. *p = 4;
D. if (p == nullptr) std::cout << "empty";

49 Consider class A { int* p; public: A(int v) : p(&v) {} int get() const { return *p; } };. What is the fundamental defect?

Pointers as class members Hard
A. A pointer member must always be initialized with new
B. v is destroyed when the constructor finishes
C. A const member function cannot dereference p
D. p cannot point to an int

50 Given class C { public: int n; void f() { n += 2; } }; C obj; C* p = &obj;, which expression correctly invokes f through p?

Pointer to objects Hard
A. (*p).f();
B. p->*f();
C. &p->f();
D. p.f();

51 Inside a non-static member function, what does this represent?

This pointer Hard
A. A pointer to the class type itself
B. A pointer to the function currently being executed
C. A reference to the current object's first member
D. A pointer to the current object

52 For struct S { int x; }; int S::* pm = &S::x; S s{12};, which expression evaluates to 12?

Pointer to data member Hard
A. *pm
B. s->*pm
C. &s.*pm
D. s.*pm

53 For void f(int a[][3], int rows), which statement about passing int m[2][3] to f(m, 2) is correct?

Array declaration and processing of multidimensional arrays inside main and inside class Hard
A. Both dimensions may be omitted because arrays carry size metadata
B. Only the first dimension may be omitted in the parameter
C. The second dimension must be passed separately at runtime
D. The compiler passes all dimensions by value

54 Inside class Matrix, a member is declared as int a[2][3];. Which definition correctly initializes all elements through a constructor initializer list?

Array declaration and processing of multidimensional arrays inside main and inside class Hard
A. Matrix() : a = {{1, 2, 3}, {4, 5, 6}} {}
B. Matrix() : a(1, 2, 3, 4, 5, 6) {}
C. Matrix() : a{{1, 2, 3}, {4, 5, 6}} {}
D. Matrix() : a[2][3]{1, 2, 3, 4, 5, 6} {}

55 Given class B { public: B(); B(int); }; B a[3] = {B(1), B(2)};, what is required for this declaration to be valid?

Array of objects Hard
A. A virtual destructor
B. A default constructor for the remaining element
C. A pointer constructor for the array
D. Only a copy constructor

56 For std::string s = "abc";, which operation has linear worst-case complexity in the current string length?

Standard C++ string class Hard
A. s[1]
B. s.size()
C. s += "xyz"
D. s.empty()

57 What is the result of std::string s = "ABCDE"; std::string t(s, 1, 3); s = t;?

Defining and assigning string objects Hard
A. s becomes "BCDE"
B. s becomes "ABC"
C. s becomes "BCD"
D. s becomes "CDE"

58 What does std::string s = "abcdef"; s.erase(2, 2); produce?

Modifiers of string class Hard
A. "cdef"
B. "abef"
C. "abcf"
D. "abcd"

59 What is the final value of std::string s = "one two"; s.replace(4, 3, "three");?

Modifiers of string class Hard
A. "one threetwo"
B. "three two"
C. "one two three"
D. "one three"

60 For std::string s = "a,b,,c";, which expression returns the position of the second comma?

Standard C++ string class Hard
A. s.find(',', 1)
B. s.find_first_not_of(',', s.find(','))
C. s.find(',', s.find(',') + 1)
D. s.rfind(',', 1)