forked from rougier/CPP-Crash-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam.html
225 lines (204 loc) · 19.5 KB
/
exam.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.8: http://docutils.sourceforge.net/" />
<title>C++ exam</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
</head>
<body>
<div class="document" id="c-exam">
<h1 class="title">C++ exam</h1>
<p>Name:</p>
<p>Surname:</p>
<div class="section" id="extern">
<h1>1 Extern</h1>
<p>The code below declares and defines variable x. <strong>True</strong> or <strong>False</strong> ?</p>
<pre class="code c++ literal-block">
<span class="k">extern</span> <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
</pre>
</div>
<div class="section" id="namespace">
<h1>2 Namespace</h1>
<p>In namespace foo, the function bar can access the variable x also declared in
namespace foo ? <strong>True</strong> or <strong>False</strong> ?</p>
<pre class="code c++ literal-block">
<span class="cp">#include <iostream>
</span>
<span class="k">namespace</span> <span class="n">foo</span>
<span class="p">{</span>
<span class="kt">void</span> <span class="n">bar</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">x</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="references">
<h1>3 References</h1>
<p>What is the output of the following program ?</p>
<pre class="code c++ literal-block">
<span class="cp">#include <iostream>
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
<span class="kt">void</span> <span class="n">swap1</span><span class="p">(</span> <span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span> <span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">c</span><span class="o">=</span><span class="n">a</span><span class="p">;</span> <span class="n">a</span><span class="o">=</span><span class="n">b</span><span class="p">;</span> <span class="n">b</span><span class="o">=</span><span class="n">c</span><span class="p">;</span> <span class="p">}</span>
<span class="kt">void</span> <span class="n">swap2</span><span class="p">(</span> <span class="kt">int</span> <span class="o">*</span><span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">b</span> <span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">c</span><span class="o">=*</span><span class="n">a</span><span class="p">;</span> <span class="o">*</span><span class="n">a</span><span class="o">=*</span><span class="n">b</span><span class="p">;</span> <span class="o">*</span><span class="n">b</span><span class="o">=</span><span class="n">c</span><span class="p">;</span> <span class="p">}</span>
<span class="kt">void</span> <span class="n">swap3</span><span class="p">(</span> <span class="kt">int</span> <span class="o">&</span><span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="o">&</span><span class="n">b</span> <span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="o">&</span><span class="n">c</span><span class="o">=</span><span class="n">a</span><span class="p">;</span> <span class="n">a</span><span class="o">=</span><span class="n">b</span><span class="p">;</span> <span class="n">b</span><span class="o">=</span><span class="n">c</span><span class="p">;</span> <span class="p">}</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span> <span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">;</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">swap1</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">);</span>
<span class="n">cout</span> <span class="o"><<</span> <span class="s">"a: "</span> <span class="o"><<</span> <span class="n">a</span> <span class="o"><<</span> <span class="s">", "</span> <span class="o"><<</span><span class="s">"b: "</span> <span class="o"><<</span> <span class="n">b</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">swap2</span><span class="p">(</span><span class="o">&</span><span class="n">a</span><span class="p">,</span><span class="o">&</span><span class="n">b</span><span class="p">);</span>
<span class="n">cout</span> <span class="o"><<</span> <span class="s">"a: "</span> <span class="o"><<</span> <span class="n">a</span> <span class="o"><<</span> <span class="s">", "</span> <span class="o"><<</span><span class="s">"b: "</span> <span class="o"><<</span> <span class="n">b</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">swap3</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">);</span>
<span class="n">cout</span> <span class="o"><<</span> <span class="s">"a: "</span> <span class="o"><<</span> <span class="n">a</span> <span class="o"><<</span> <span class="s">", "</span> <span class="o"><<</span><span class="s">"b: "</span> <span class="o"><<</span> <span class="n">b</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="inheritance">
<h1>4 Inheritance</h1>
<p>What is the output of the program ?</p>
<pre class="code c++ literal-block">
<span class="cp">#include <iostream>
</span>
<span class="k">struct</span> <span class="n">A</span> <span class="p">{</span> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">color</span><span class="p">;</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">B</span> <span class="o">:</span> <span class="k">public</span> <span class="n">A</span> <span class="p">{</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">C</span> <span class="o">:</span> <span class="k">public</span> <span class="n">A</span> <span class="p">{</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">D</span> <span class="o">:</span> <span class="k">public</span> <span class="n">B</span><span class="p">,</span> <span class="k">public</span> <span class="n">C</span> <span class="p">{</span> <span class="p">};</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">D</span> <span class="n">d</span><span class="p">;</span>
<span class="n">d</span><span class="p">.</span><span class="n">color</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">d</span><span class="p">.</span><span class="n">color</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="id1">
<h1>5 Inheritance</h1>
<p>How many times is "Hello World" printed by this program ?</p>
<pre class="code c++ literal-block">
<span class="cp">#include <iostream>
</span>
<span class="k">struct</span> <span class="n">A</span> <span class="p">{</span> <span class="n">A</span><span class="p">()</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Hello World"</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">A1</span> <span class="o">:</span> <span class="k">public</span> <span class="n">A</span> <span class="p">{</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">A2</span> <span class="o">:</span> <span class="k">public</span> <span class="n">A</span> <span class="p">{</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">A3</span> <span class="o">:</span> <span class="k">public</span> <span class="n">A</span> <span class="p">{</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">A4</span> <span class="o">:</span> <span class="k">public</span> <span class="n">A</span> <span class="p">{</span> <span class="p">};</span>
<span class="k">struct</span> <span class="n">B</span> <span class="o">:</span> <span class="k">public</span> <span class="n">A1</span><span class="p">,</span> <span class="k">public</span> <span class="n">A2</span><span class="p">,</span> <span class="k">public</span> <span class="n">A3</span><span class="p">,</span> <span class="k">public</span> <span class="n">A4</span> <span class="p">{</span> <span class="p">};</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">B</span> <span class="n">b</span><span class="p">;</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="initialization">
<h1>6 Initialization</h1>
<p>What is the value of x, y & z ?</p>
<pre class="code c++ literal-block">
<span class="cp">#include <iostream>
</span>
<span class="k">struct</span> <span class="n">A</span>
<span class="p">{</span>
<span class="n">A</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="o">:</span> <span class="n">x</span><span class="p">(</span><span class="n">n</span><span class="o">++</span><span class="p">),</span> <span class="n">y</span><span class="p">(</span><span class="n">n</span><span class="o">++</span><span class="p">),</span> <span class="n">z</span><span class="p">(</span><span class="n">n</span><span class="o">++</span><span class="p">)</span> <span class="p">{}</span>
<span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">z</span><span class="p">;</span>
<span class="p">};</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">Foo</span> <span class="n">f</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"x: "</span> <span class="o"><<</span> <span class="n">f</span><span class="p">.</span><span class="n">x</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"y: "</span> <span class="o"><<</span> <span class="n">f</span><span class="p">.</span><span class="n">y</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"z: "</span> <span class="o"><<</span> <span class="n">f</span><span class="p">.</span><span class="n">z</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="logic">
<h1>7 Logic</h1>
<p>What value gets printed by the program?</p>
<pre class="code c++ literal-block">
<span class="cp">#include <iostream>
</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="o">++</span> <span class="o">&&</span> <span class="n">y</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">y</span> <span class="o">+=</span> <span class="mi">2</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="constructors">
<h1>8 Constructors</h1>
<p>Which lines below should not compile ?</p>
<pre class="code c++ literal-block">
<span class="k">struct</span> <span class="n">A</span>
<span class="p">{</span>
<span class="n">A</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="o">:</span> <span class="n">n</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{}</span>
<span class="kt">int</span> <span class="n">n</span><span class="p">;</span>
<span class="p">};</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">A</span> <span class="n">a1</span><span class="p">;</span>
<span class="n">A</span> <span class="n">a2</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="n">A</span> <span class="n">a3</span><span class="p">(</span><span class="n">a2</span><span class="p">);</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="memory">
<h1>9 Memory</h1>
<p>Which of the following implementations of the reset function is best for
initializing the array to all zero.</p>
<pre class="code c++ literal-block">
<span class="k">class</span> <span class="nc">foo</span><span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="n">foo</span><span class="p">(){</span>
<span class="n">reset</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">private</span><span class="o">:</span>
<span class="kt">void</span> <span class="n">reset</span><span class="p">(){</span>
<span class="c1">// A // memset(x, 0, 50);
</span> <span class="c1">// B // memset(x, 0, sizeof(x));
</span> <span class="c1">// C // memset(x, 0, 50 * 4);
</span> <span class="c1">// D // memset(x, 0, 50 * sizeof(x));
</span> <span class="p">}</span>
<span class="kt">long</span> <span class="n">x</span><span class="p">[</span><span class="mi">50</span><span class="p">];</span>
<span class="p">};</span>
</pre>
</div>
<div class="section" id="id2">
<h1>10 References</h1>
<p>What is the output of the program ?</p>
<pre class="code c++ literal-block">
<span class="cp">#include <iostream>
</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// assume address of x is 0x822222222
</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
<span class="kt">int</span><span class="o">*&</span> <span class="n">rpx</span> <span class="o">=</span> <span class="o">&</span><span class="n">x</span><span class="p">;</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">rpx</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<div class="section" id="end">
<h1>11 End</h1>
</div>
</div>
</body>
</html>