-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.html
More file actions
342 lines (334 loc) · 33.7 KB
/
Copy pathrules.html
File metadata and controls
342 lines (334 loc) · 33.7 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Java code questions — plain English rules</title><style>
:root{--bg:#fff;--ink:#15181d;--mut:#525a64;--faint:#8a929c;--line:#e2e6ea;--acc:#c25e12;
--ok:#12693e;--okbg:#eaf6ef;--code:#1d2531;--codeink:#e9ecf1}
*{box-sizing:border-box}
body{margin:0;background:#f6f7f9;color:var(--ink);
font:16px/1.6 "Inter",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Arial,sans-serif}
.wrap{max-width:860px;margin:0 auto;padding:26px 18px 70px}
header{margin-bottom:22px}
h1{font-size:1.7rem;margin:0 0 6px;letter-spacing:-.02em}
header p{margin:0;color:var(--mut)}
.big{background:#fdf3e8;border-left:4px solid var(--acc);border-radius:0 10px 10px 0;
padding:14px 18px;margin:18px 0 26px;font-size:1.02rem}
.big b{color:var(--acc)}
section{background:#fff;border:1px solid var(--line);border-radius:14px;
padding:20px 22px 6px;margin-bottom:20px}
h2{font-size:1.18rem;margin:0 0 4px;display:flex;align-items:center;gap:10px;letter-spacing:-.01em}
.ltr{background:var(--acc);color:#fff;width:26px;height:26px;border-radius:7px;
display:grid;place-items:center;font-size:.85rem;font-weight:800;flex:none}
.intro{color:var(--mut);margin:0 0 18px;font-size:.94rem}
article{border-top:1px solid var(--line);padding:16px 0 18px}
h3{font-size:1.03rem;margin:0 0 6px;font-weight:700}
.plain{margin:0 0 12px;color:var(--ink)}
.qbox{background:#fafbfc;border:1px solid var(--line);border-radius:10px;padding:13px 15px}
.qlab{font-size:.72rem;text-transform:uppercase;letter-spacing:.07em;color:var(--faint);
font-weight:700;margin-bottom:7px}
.qtext{margin:0 0 9px;font-weight:600;font-size:.96rem}
pre{background:var(--code);color:var(--codeink);border-radius:8px;padding:11px 13px;margin:0 0 10px;
overflow-x:auto;font-family:"Cascadia Code",Consolas,"SF Mono",Menlo,monospace;
font-size:.82rem;line-height:1.5;white-space:pre}
pre.mini{margin:5px 0 0;font-size:.76rem;padding:7px 10px}
pre .k{color:#ff9d5c}pre .s{color:#8fe37a}pre .c{color:#7d8794;font-style:italic}
ol.opts{list-style:none;margin:0 0 10px;padding:0}
ol.opts li{display:flex;align-items:flex-start;gap:9px;padding:7px 10px;border-radius:7px;
border:1px solid var(--line);background:#fff;margin-bottom:5px;font-size:.93rem}
ol.opts li.right{border-color:var(--ok);background:var(--okbg)}
.lk{flex:none;width:21px;height:21px;border-radius:5px;background:#eef0f2;display:grid;
place-items:center;font-size:.74rem;font-weight:800;text-transform:uppercase}
li.right .lk{background:var(--ok);color:#fff}
.lt{flex:1}
.tick{color:var(--ok);font-weight:700;font-size:.78rem;white-space:nowrap;align-self:center}
.why{margin:0;font-size:.92rem;color:var(--mut);background:#fff;border-left:3px solid var(--acc);
padding:8px 12px;border-radius:0 7px 7px 0}
.why b{color:var(--acc)}
code{background:#eef0f2;border-radius:4px;padding:1px 5px;font-size:.88em;
font-family:"Cascadia Code",Consolas,Menlo,monospace}
footer{color:var(--faint);font-size:.84rem;text-align:center;padding:22px 10px}
@media print{
body{background:#fff} .wrap{max-width:none;padding:0}
section{break-inside:auto;border:none;padding:0;margin-bottom:14px}
article{break-inside:avoid}
pre{background:#f2f3f5;color:#15181d}
pre .k{color:#a8430b}pre .s{color:#14663c}pre .c{color:#6b7280}
}
</style></head><body><div class="wrap"><header><h1>Java code questions — the rules, in plain English</h1><p>Every rule below is followed by a real question from your quiz bank.</p></header><div class="big"><b>If you are stuck, pick “Compilation Error”.</b> It is the correct answer for about 1 in 6 of these code questions — more than any other single answer. Inside the overloading and overriding topics it is nearly 40%.</div><section><h2><span class="ltr">A</span>When the answer is “Compilation Error”</h2><p class="intro">This is the most common answer in the whole set — about 1 in 6. Learn these giveaways and you win those marks in five seconds, without working through the code.</p><article><h3>A class tries to extend two classes</h3><p class="plain">Java lets a class have only ONE parent. Two names after <code>extends</code> means it will not compile.</p><div class="qbox"><div class="qlab">Real question — <em>Derived Class</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> Base
{
<span class="k">void</span> msg()
{
System.out.println(<span class="s">"Hello"</span>);
}
}
<span class="k">class</span> Intermediate
{
<span class="k">void</span> msg()
{
System.out.println(<span class="s">"Welcome"</span>);
}
}
<span class="k">class</span> Derived <span class="k">extends</span> Base,Intermediate
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
C obj=<span class="k">new</span> C();
obj.msg();
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt"><em>(see code)</em><pre class="mini">Hello
Welcome</pre></span></li><li class=""><span class="lk">b</span><span class="lt"><em>(see code)</em><pre class="mini">Welcome
Hello</pre></span></li><li class="right"><span class="lk">c</span><span class="lt">Compilation error</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">Hello</span></li></ol><p class="why"><b>Why:</b> Java does not allow multiple inheritance for classes. One parent only.</p></div></article><article><h3>Someone extends a <code>final</code> class</h3><p class="plain"><code>final</code> on a class means “nobody may inherit from me”.</p><div class="qbox"><div class="qlab">Real question — <em>Final Class</em></div><p class="qtext">What is the output of the following Java code?</p><pre><span class="k">final</span> <span class="k">class</span> Final
{
<span class="k">static</span> String s = <span class="s">"Sanfoundry"</span>;
}
<span class="k">public</span> <span class="k">class</span> Program <span class="k">extends</span> Final
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
System.out.println(s);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">Garbage Value</span></li><li class=""><span class="lk">b</span><span class="lt">Sanfoundry</span></li><li class="right"><span class="lk">c</span><span class="lt">Compilation Error</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">Runtime Error</span></li></ol><p class="why"><b>Why:</b> The class is marked final, so <code>extends Final</code> is illegal.</p></div></article><article><h3>Someone overrides a <code>final</code> method</h3><p class="plain">Same idea one level down: a <code>final</code> method cannot be replaced by a child class.</p><div class="qbox"><div class="qlab">Real question — <em>Method Overriding</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> B
{
<span class="k">protected</span> <span class="k">final</span> <span class="k">void</span> Display()
{
System.out.println(<span class="s">"B"</span>);
}
}
<span class="k">public</span> <span class="k">class</span> C <span class="k">extends</span> B
{
<span class="k">protected</span> <span class="k">final</span> <span class="k">void</span> Display()
{
System.out.println(<span class="s">"C"</span>);
}
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
B obj = <span class="k">new</span> B();
obj.Display();
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">B</span></li><li class=""><span class="lk">b</span><span class="lt">C</span></li><li class=""><span class="lk">c</span><span class="lt">Runtime error</span></li><li class="right"><span class="lk">d</span><span class="lt">Compilation error</span><span class="tick">✔ correct</span></li></ol><p class="why"><b>Why:</b> Display() is final in B, so C is not allowed to redefine it.</p></div></article><article><h3>A child touches a <code>private</code> member of its parent</h3><p class="plain"><code>private</code> means only that class can see it. Children get nothing. Most people wrongly assume private fields are inherited.</p><div class="qbox"><div class="qlab">Real question — <em>Hierarchical Inheritance</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> A
{
<span class="k">private</span> <span class="k">int</span> a = 10;
}
<span class="k">class</span> B <span class="k">extends</span> A
{
<span class="k">int</span> b = a;
}
<span class="k">class</span> C <span class="k">extends</span> A
{
<span class="k">int</span> c = a;
}
<span class="k">public</span> <span class="k">class</span> PrivateVariable
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
B obj1 = <span class="k">new</span> B();
C obj2 = <span class="k">new</span> C();
System.out.println(obj1.b+obj2.c);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">20</span></li><li class=""><span class="lk">b</span><span class="lt">30</span></li><li class=""><span class="lk">c</span><span class="lt">40</span></li><li class="right"><span class="lk">d</span><span class="lt">Compile time Error</span><span class="tick">✔ correct</span></li></ol><p class="why"><b>Why:</b> <code>a</code> is private in A, so B cannot use it — even though B extends A.</p></div></article><article><h3>A <code>static</code> thing uses a non-static thing</h3><p class="plain"><code>static</code> belongs to the class; ordinary fields belong to an object. A static method has no object, so it cannot read an ordinary field.</p><div class="qbox"><div class="qlab">Real question — <em>Nested Class</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> Outer
{
<span class="k">int</span> x=10;
<span class="k">static</span> <span class="k">class</span> Nested
{
<span class="k">int</span> y = 10;
<span class="k">static</span> <span class="k">void</span> Calc()
{
System.out.println(x+y);
}
}
}
<span class="k">class</span> Calculation
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
Outer.Nested.Calc();
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">10</span></li><li class=""><span class="lk">b</span><span class="lt">20</span></li><li class="right"><span class="lk">c</span><span class="lt">Compilation Error</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">Runtime Error</span></li></ol><p class="why"><b>Why:</b> Calc() is static, so it has no object to read the non-static <code>x</code> from.</p></div></article><article><h3><code>new Thing()</code> when there is no empty constructor</h3><p class="plain">The moment you write any constructor yourself, Java stops giving you the free empty one.</p><div class="qbox"><div class="qlab">Real question — <em>Constructor Overloading</em></div><p class="qtext">What is the output of the following Java code?</p><pre><span class="k">class</span> Sum
{
<span class="k">private</span> <span class="k">int</span> num;
Sum(<span class="k">int</span> rn)
{
num = num + rn;
}
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
Sum obj = <span class="k">new</span> Sum();
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">0</span></li><li class=""><span class="lk">b</span><span class="lt">Null</span></li><li class="right"><span class="lk">c</span><span class="lt">Compilation Error</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">Garbage Value</span></li></ol><p class="why"><b>Why:</b> Sum only has a constructor taking an int, so <code>new Sum()</code> with no argument matches nothing.</p></div></article><article><h3>A variable used outside the <code>{ }</code> it was born in</h3><p class="plain">A variable declared inside braces dies at the closing brace.</p><div class="qbox"><div class="qlab">Real question — <em>Literals & Variables</em></div><p class="qtext">What will be the output of the following Java program?</p><pre><span class="k">class</span> variable_scope
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
<span class="k">int</span> x;
x = 5;
{
<span class="k">int</span> y = 6;
System.out.print(x + <span class="s">" "</span> + y);
}
System.out.println(x + <span class="s">" "</span> + y);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">5 6 5 6</span></li><li class=""><span class="lk">b</span><span class="lt">5 6 5</span></li><li class=""><span class="lk">c</span><span class="lt">Runtime error</span></li><li class="right"><span class="lk">d</span><span class="lt">Compilation error</span><span class="tick">✔ correct</span></li></ol><p class="why"><b>Why:</b> <code>y</code> was declared inside the inner block, so the second print cannot see it.</p></div></article><article><h3>Maths on <code>byte</code>/<code>short</code> put back without a cast</h3><p class="plain">Arithmetic on small types is done as <code>int</code> first. Putting that <code>int</code> back into a <code>byte</code> needs an explicit cast.</p><div class="qbox"><div class="qlab">Real question — <em>Type Conversions, Promotions and Castings</em></div><p class="qtext">What will be the error in the following Java code?</p><pre><span class="k">byte</span> b = 50;
b = b * 50;</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">b cannot contain value 100, limited by its range</span></li><li class="right"><span class="lk">b</span><span class="lt">* operator has converted b * 50 into int, which can not be converted to byte without casting</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">c</span><span class="lt">b cannot contain value 50</span></li><li class=""><span class="lk">d</span><span class="lt">No error in this code</span></li></ol><p class="why"><b>Why:</b> <code>b * 50</code> becomes an int, and an int will not fit back into a byte without <code>(byte)</code>.</p></div></article><article><h3>Two overloads and the compiler cannot choose</h3><p class="plain">If two versions of a method match your arguments equally well, Java refuses to guess.</p><div class="qbox"><div class="qlab">Real question — <em>Overloading Methods & Argument Passing</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> San
{
<span class="k">public</span> <span class="k">void</span> m1 (<span class="k">int</span> i,<span class="k">float</span> f)
{
System.out.println(<span class="s">" int float method"</span>);
}
<span class="k">public</span> <span class="k">void</span> m1(<span class="k">float</span> f,<span class="k">int</span> i);
{
System.out.println(<span class="s">"float int method"</span>);
}
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[]args)
{
San s=<span class="k">new</span> San();
s.m1(20,20);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">int float method</span></li><li class=""><span class="lk">b</span><span class="lt">float int method</span></li><li class="right"><span class="lk">c</span><span class="lt">compile time error</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">run time error</span></li></ol><p class="why"><b>Why:</b> Both m1 versions could match after promotion, so the compiler calls it ambiguous.</p></div></article></section><section><h2><span class="ltr">B</span>Inheritance — which version actually runs</h2><p class="intro">About 95 questions, nearly all of them one of these four patterns.</p><article><h3>Parent label, child object → the CHILD’s method runs</h3><p class="plain">The most tested idea in the set. Look for <code>Parent p = new Child();</code>. For a <b>method</b>, Java uses the object you really made, not the label on the left.</p><div class="qbox"><div class="qlab">Real question — <em>Base Class</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> Base
{
<span class="k">public</span> <span class="k">void</span> display()
{
System.out.println(<span class="s">"Base class called"</span>);
}
}
<span class="k">class</span> Derived <span class="k">extends</span> Base
{
<span class="k">public</span> <span class="k">void</span> display()
{
System.out.println(<span class="s">"Derived class called"</span>);
}
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
Base obj = <span class="k">new</span> Derived();
obj.display();
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">Base class called</span></li><li class="right"><span class="lk">b</span><span class="lt">Derived class called</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">c</span><span class="lt"><em>(see code)</em><pre class="mini">Base <span class="k">class</span> called
Derived <span class="k">class</span> called</pre></span></li><li class=""><span class="lk">d</span><span class="lt"><em>(see code)</em><pre class="mini">Derived <span class="k">class</span> called
Base <span class="k">class</span> called</pre></span></li></ol><p class="why"><b>Why:</b> <code>obj</code> is labelled Base but is really a Derived, so Derived’s display() runs.</p></div></article><article><h3><code>static</code> methods do the opposite — they follow the label</h3><p class="plain">Static methods are not overridden, only hidden. So a static call uses the type written on the LEFT, not the real object.</p><div class="qbox"><div class="qlab">Real question — <em>Base Class</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> Base
{
<span class="k">static</span> <span class="k">void</span> staticMethod()
{
System.out.println(<span class="s">"Class X"</span>);
}
}
<span class="k">class</span> Derived <span class="k">extends</span> Base
{
<span class="k">static</span> <span class="k">void</span> staticMethod()
{
System.out.println(<span class="s">"Class Y"</span>);
}
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
Base.staticMethod();
}
}</pre><ol class="opts"><li class="right"><span class="lk">a</span><span class="lt">Class X</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">b</span><span class="lt">Class Y</span></li><li class=""><span class="lk">c</span><span class="lt">Compilation error</span></li><li class=""><span class="lk">d</span><span class="lt"><em>(see code)</em><pre class="mini">Class X
Class Y</pre></span></li></ol><p class="why"><b>Why:</b> The reference is of type Base, and static methods are chosen by the label, so Base’s version runs.</p></div></article><article><h3>Printing an object gives <code>ClassName@hashcode</code></h3><p class="plain">If a class has no <code>toString()</code>, printing the object shows the class name and a hash. The name shown is the REAL class.</p><div class="qbox"><div class="qlab">Real question — <em>Types of Inheritance</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> A
{
<span class="k">int</span> a = 50;
}
<span class="k">class</span> B <span class="k">extends</span> A
{
<span class="k">int</span> a = 100;
}
<span class="k">public</span> <span class="k">class</span> PrintObject
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
A b = <span class="k">new</span> B();
System.out.println(b);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">50, 100</span></li><li class=""><span class="lk">b</span><span class="lt">100, 50</span></li><li class="right"><span class="lk">c</span><span class="lt">B@2a139a55</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">A$B@2a139a55</span></li></ol><p class="why"><b>Why:</b> The object is printed directly, so you get ClassName@hashcode — and the real class is B.</p></div></article><article><h3>Ordinary (non-private) fields ARE inherited</h3><p class="plain">The child can use its parent’s normal fields directly. Only <code>private</code> ones are off limits.</p><div class="qbox"><div class="qlab">Real question — <em>Base Class</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> Worker
{
<span class="k">float</span> salary=98000;
}
<span class="k">class</span> Manager <span class="k">extends</span> Worker
{
<span class="k">int</span> bonus=25000;
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
Manager p=<span class="k">new</span> Manager();
System.out.println(+p.salary);
}
}</pre><ol class="opts"><li class="right"><span class="lk">a</span><span class="lt">98000.0</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">b</span><span class="lt">98000</span></li><li class=""><span class="lk">c</span><span class="lt">Compilation error</span></li><li class=""><span class="lk">d</span><span class="lt">25000</span></li></ol><p class="why"><b>Why:</b> <code>salary</code> is an ordinary field in Worker, so Manager can read it. It is a float, so it prints 98000.0 not 98000.</p></div></article></section><section><h2><span class="ltr">C</span>Constructors</h2><p class="intro">About 60 questions, and they repeat heavily.</p><article><h3><code>this.x</code> means “this object’s x”</h3><p class="plain">Use it when a parameter or local variable has the same name as the field, to say which one you mean.</p><div class="qbox"><div class="qlab">Real question — <em>Constructors</em></div><p class="qtext">What is the output of the following Java code?</p><pre><span class="k">class</span> Main
{
<span class="k">int</span> x=5;
Main()
{
<span class="k">this</span>.x = 7;
}
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String[] args)
{
Main obj = <span class="k">new</span> Main();
System.out.println(obj.x);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">5</span></li><li class=""><span class="lk">b</span><span class="lt">Compilation error</span></li><li class=""><span class="lk">c</span><span class="lt">12</span></li><li class="right"><span class="lk">d</span><span class="lt">7</span><span class="tick">✔ correct</span></li></ol><p class="why"><b>Why:</b> <code>this.x = 7</code> sets the object’s own field to 7, so printing it gives 7.</p></div></article><article><h3>Write one constructor and the free one disappears</h3><p class="plain">Java only gives a no-argument constructor if you write none at all. This is the biggest single cause of “Compilation Error” in the constructor topics.</p><div class="qbox"><div class="qlab">Real question — <em>Constructor Overloading</em></div><p class="qtext">What is the output of the following Java code?</p><pre><span class="k">class</span> Sum
{
<span class="k">private</span> <span class="k">int</span> num;
Sum(<span class="k">int</span> rn)
{
num = num + rn;
}
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
Sum obj = <span class="k">new</span> Sum();
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">0</span></li><li class=""><span class="lk">b</span><span class="lt">Null</span></li><li class="right"><span class="lk">c</span><span class="lt">Compilation Error</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">Garbage Value</span></li></ol><p class="why"><b>Why:</b> Worth seeing twice — this exact trap appears again and again.</p></div></article></section><section><h2><span class="ltr">D</span>Numbers</h2><p class="intro">A small set of facts that keep coming back.</p><article><h3>Dividing by zero depends on the type</h3><p class="plain">Whole numbers crash. Decimals do not — they give <code>Infinity</code> or <code>NaN</code>.</p><div class="qbox"><div class="qlab">Real question — <em>Integer and Floating Datatypes</em></div><p class="qtext">What will be the output of the following Java statement?</p><pre><span class="k">class</span> output {
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
<span class="k">double</span> a, b,c;
a = 3.0/0;
b = 0/4.0;
c=0/0.0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">Infinity</span></li><li class=""><span class="lk">b</span><span class="lt">0.0</span></li><li class=""><span class="lk">c</span><span class="lt">NaN</span></li><li class="right"><span class="lk">d</span><span class="lt">all of the mentioned</span><span class="tick">✔ correct</span></li></ol><p class="why"><b>Why:</b> With doubles you get Infinity and NaN instead of a crash, so every listed result is correct.</p></div></article><article><h3><code>++i</code> adds first, <code>i++</code> adds after</h3><p class="plain"><code>++g</code> increases g and then uses the new value. <code>g++</code> uses the old value and increases afterwards.</p><div class="qbox"><div class="qlab">Real question — <em>Arithmetic Operators</em></div><p class="qtext">What will be the output of the following Java program?</p><pre><span class="k">class</span> increment
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
<span class="k">int</span> g = 3;
System.out.print(++g * 8);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">25</span></li><li class=""><span class="lk">b</span><span class="lt">24</span></li><li class="right"><span class="lk">c</span><span class="lt">32</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">33</span></li></ol><p class="why"><b>Why:</b> <code>++g</code> makes g 4 before multiplying, so the answer is 32 and not 24.</p></div></article><article><h3><code>char</code> maths turns into numbers</h3><p class="plain">A char is secretly a number (its character code). Add to it or cast it to <code>int</code> and you see that number.</p><div class="qbox"><div class="qlab">Real question — <em>Character and Boolean Datatypes</em></div><p class="qtext">What will be the output of the following Java program?</p><pre><span class="k">class</span> mainclass {
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
<span class="k">char</span> a = <span class="s">'A'</span>;
a++;
System.out.print((<span class="k">int</span>)a);
}
}</pre><ol class="opts"><li class="right"><span class="lk">a</span><span class="lt">66</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">b</span><span class="lt">67</span></li><li class=""><span class="lk">c</span><span class="lt">65</span></li><li class=""><span class="lk">d</span><span class="lt">64</span></li></ol><p class="why"><b>Why:</b> ‘A’ is 65; <code>a++</code> makes it 66, and casting to int shows 66.</p></div></article></section><section><h2><span class="ltr">E</span>Strings, objects and arrays</h2><p class="intro">The last cluster worth knowing.</p><article><h3><code>.equals()</code> compares contents, <code>==</code> compares identity</h3><p class="plain"><code>==</code> asks “are these the same object?”. <code>.equals()</code> asks “do they hold the same thing?”. For text and lists you almost always want equals.</p><div class="qbox"><div class="qlab">Real question — <em>Array Class Under Java</em></div><p class="qtext">What will be the output of the following Java program?</p><pre><span class="k">import</span> java.util.*;
<span class="k">class</span> Arraylist
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
ArrayList obj1 = <span class="k">new</span> ArrayList();
ArrayList obj2 = <span class="k">new</span> ArrayList();
obj1.add(<span class="s">"A"</span>);
obj1.add(<span class="s">"B"</span>);
obj2.add(<span class="s">"A"</span>);
obj2.add(1, <span class="s">"B"</span>);
System.out.println(obj1.equals(obj2));
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">0</span></li><li class=""><span class="lk">b</span><span class="lt">1</span></li><li class="right"><span class="lk">c</span><span class="lt">true</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">d</span><span class="lt">false</span></li></ol><p class="why"><b>Why:</b> The two lists are separate objects, but they hold the same items, so <code>equals()</code> is true.</p></div></article><article><h3>String methods are case-sensitive</h3><p class="plain"><code>startsWith</code>, <code>equals</code> and <code>compareTo</code> all treat capitals as different letters.</p><div class="qbox"><div class="qlab">Real question — <em>String Comparision</em></div><p class="qtext">What will be the output of the following Java code?</p><pre><span class="k">class</span> output
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
String c = <span class="s">"Hello i love java"</span>;
<span class="k">boolean</span> var;
var = c.startsWith(<span class="s">"hello"</span>);
System.out.println(var);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">true</span></li><li class="right"><span class="lk">b</span><span class="lt">false</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">c</span><span class="lt">0</span></li><li class=""><span class="lk">d</span><span class="lt">1</span></li></ol><p class="why"><b>Why:</b> The text starts with a capital H, so checking for lowercase “hello” gives false.</p></div></article><article><h3><code>StringBuffer</code> changes the original object</h3><p class="plain">Strings never change — every operation returns a new one. A StringBuffer is different: it edits itself in place and returns itself.</p><div class="qbox"><div class="qlab">Real question — <em>StringBuffer Class</em></div><p class="qtext">What will be the output of the following Java program?</p><pre><span class="k">class</span> output
{
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> main(String args[])
{
StringBuffer s1 = <span class="k">new</span> StringBuffer(<span class="s">"Hello"</span>);
StringBuffer s2 = s1.reverse();
System.out.println(s2);
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">Hello</span></li><li class="right"><span class="lk">b</span><span class="lt">olleH</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">c</span><span class="lt">HelloolleH</span></li><li class=""><span class="lk">d</span><span class="lt">olleHHello</span></li></ol><p class="why"><b>Why:</b> reverse() reverses the buffer and hands back the same object, so s2 shows the reversed text.</p></div></article><article><h3>Array indexes start at 0 — watch the loop bounds</h3><p class="plain">Counting from 1, or stopping at <code>length - 2</code>, silently skips items. Trace the loop, do not assume.</p><div class="qbox"><div class="qlab">Real question — <em>Command Line Arguments – 2</em></div><p class="qtext">What will be the output of the following Java snippet, if compiled and executed with command line argument “java abc 1 2 3”?</p><pre><span class="k">public</span> <span class="k">class</span> abc
{
<span class="k">static</span> <span class="k">public</span> <span class="k">void</span> main(String [] xyz)
{
<span class="k">for</span>(<span class="k">int</span> n=1;n<xyz.length; n++)
{
System.out.println(xyz[n]+<span class="s">""</span>);
}
}
}</pre><ol class="opts"><li class=""><span class="lk">a</span><span class="lt">1 2</span></li><li class="right"><span class="lk">b</span><span class="lt">2 3</span><span class="tick">✔ correct</span></li><li class=""><span class="lk">c</span><span class="lt">1 2 3</span></li><li class=""><span class="lk">d</span><span class="lt">Compilation error</span></li></ol><p class="why"><b>Why:</b> The loop starts at 1 instead of 0, so the first item is skipped.</p></div></article></section><footer>Questions and answers are Sanfoundry's own, taken from your 510-question <em>Java basics + all OOP</em> code set.</footer></div></body></html>