Unit 2: Handling Pointers, Arrays and String - Practice Quiz
1 Which symbol is used to declare a reference variable in C++?
#
*
%
&
2 Which type of pointer can store the address of an object of any data type?
float*
int*
void*
char*
3
If p points to an element of an integer array, what does p + 1 generally indicate?
4 Which declaration represents a pointer to a pointer to an integer?
int* p;
int p**;
int** p;
int& p;
5 What is a dangling pointer?
6 A pointer that has not been initialized is commonly called a:
7 Which value is commonly used to indicate that a pointer does not point to a valid object?
delete
nullptr
static
sizeof
8 A pointer declared inside a class is called a:
9 Which operator is used to access a member through a pointer to an object?
->
.*
::
.
10
In a non-static member function, what does the this pointer refer to?
11 Which operator is used with an object to access a pointer to a non-static data member?
%%
&&
->
.*
12 Which declaration creates a two-dimensional integer array with 2 rows and 3 columns?
int a[3][2];
int a(2)(3);
int a[2,3];
int a[2][3];
13 Which loop structure is commonly used to process every element of a two-dimensional array?
14 What is an array of objects?
15
Which header file provides the standard C++ string class?
<pointer>
<string>
<text>
<array>
16
Which statement correctly defines a C++ string object containing Hello?
string s = "Hello";
string s = Hello;
String s[Hello];
char s = "Hello";
17
Which operator is used to assign one string object to another?
+= only
=
<=
==
18 Which string member function adds characters to the end of a string?
remove()
compare()
append()
lengthen()
19 Which string member function can remove characters from a string?
copy()
find()
erase()
insert()
20 Which string member function inserts characters at a specified position?
swap()
insert()
clear()
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;
4 4
4 7
7 4
7 7
22
Given double value = 6.25; void* vp = &value;, which expression correctly reads the stored value?
*static_cast<double*>(vp)
static_cast<double>(*vp)
*static_cast<int*>(vp)
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;
6
12
3
9
24
What is the final value of n?
int n = 5; int* p = &n; int** pp = &p; **pp += 3;
5
3
8
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;
p = nullptr;
p++;
delete p; again
*p = 0;
26 Which declaration creates an uninitialized pointer that may contain an indeterminate address?
int* p;
int* p = nullptr;
int x = 0; int* p = &x;
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++?
show(0);
show(NULL);
show(nullptr);
show(false);
28
A class owns an int* data allocated with new int. Why may the compiler-generated copy constructor be unsafe?
29
Given Student s; Student* p = &s;, which statement calls the member function display() through p?
p.display();
*p.display();
p::display();
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 */ } };
value = this;
this->value = value;
*this = value;
this = &value;
31
Given struct Point { int x; }; Point pt{8}; int Point::* pm = &Point::x;, which expression obtains the value 8?
pt->*pm
*pt.pm
pm.*pt
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?
member->pb = 20;
pb.*member = 20;
*pb.member = 20;
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;
7
6
8
9
34
A class contains int matrix[3][4];. Which nested-loop bounds correctly process every element exactly once?
i <= 2 and j < 3
i <= 3 and j <= 4
i < 4 and j < 3
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;
5
20
10
25
36
Which declaration correctly creates three Book objects using a constructor Book(std::string, int)?
Book books(3) = {{"A", 100}, {"B", 120}, {"C", 140}};
Book books[3] = {{"A", 100}, {"B", 120}, {"C", 140}};
Book[3] books = {"A", 100, "B", 120, "C", 140};
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);
gramm
ogra
gram
ramm
38
What does std::string::find return when the requested substring is absent?
-1 as an int
nullptr
std::string::npos
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;
AAABC
ABCBC
AABC
3ABC
40
What is the final value of s?
std::string s = "abcdef"; s.erase(2, 2); s.insert(2, "XY");
aXYdef
abcdXY
abXYef
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?
r = &b; p = b;
p = b; r = &b;
r = b; p = &b;
p = &b; r = b;
42
What is the correct way to print the value stored in int n = 42; void* vp = &n;?
std::cout << static_cast<int*>(vp);
std::cout << *vp;
std::cout << static_cast<int>(*vp);
std::cout << *(int*)vp;
43
For int a[5] = {2, 4, 6, 8, 10}; int* p = a + 1;, which expression evaluates to 8?
*(p + 1)
p[3]
*p + 1
*(p - 1)
44
For int a[5] = {2, 4, 6, 8, 10}; int* p = a + 1;, which expression evaluates to 8?
*(p + 1)
p[2]
*p + 1
*(p - 1)
45
What is printed by int x = 5; int* p = &x; int** q = &p; **q = 9; std::cout << x;?
x
9
5
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;
p before dereferencing it
p to void* before dereferencing it
x as static inside the block
delete p after the block
47 Which declaration creates a wild pointer whose value is indeterminate until it is initialized?
int* p = new int;
int* p = nullptr + 1;
int* p = nullptr;
int* p;
48
After int* p = nullptr;, which operation is well-defined?
std::cout << p[0];
std::cout << *p;
*p = 4;
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?
new
v is destroyed when the constructor finishes
const member function cannot dereference p
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?
(*p).f();
p->*f();
&p->f();
p.f();
51
Inside a non-static member function, what does this represent?
52
For struct S { int x; }; int S::* pm = &S::x; S s{12};, which expression evaluates to 12?
*pm
s->*pm
&s.*pm
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?
54
Inside class Matrix, a member is declared as int a[2][3];. Which definition correctly initializes all elements through a constructor initializer list?
Matrix() : a = {{1, 2, 3}, {4, 5, 6}} {}
Matrix() : a(1, 2, 3, 4, 5, 6) {}
Matrix() : a{{1, 2, 3}, {4, 5, 6}} {}
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?
56
For std::string s = "abc";, which operation has linear worst-case complexity in the current string length?
s[1]
s.size()
s += "xyz"
s.empty()
57
What is the result of std::string s = "ABCDE"; std::string t(s, 1, 3); s = t;?
s becomes "BCDE"
s becomes "ABC"
s becomes "BCD"
s becomes "CDE"
58
What does std::string s = "abcdef"; s.erase(2, 2); produce?
"cdef"
"abef"
"abcf"
"abcd"
59
What is the final value of std::string s = "one two"; s.replace(4, 3, "three");?
"one threetwo"
"three two"
"one two three"
"one three"
60
For std::string s = "a,b,,c";, which expression returns the position of the second comma?
s.find(',', 1)
s.find_first_not_of(',', s.find(','))
s.find(',', s.find(',') + 1)
s.rfind(',', 1)
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →