java quiz
DESCRIPTION
This presentation is a Quiz based on basics of Java.TRANSCRIPT
JAVA Quiz
1. Which one of these lists contains only Java programming language keywords?
A class, if, void, long, Int, continue B goto, instanceof, native, finally, default,
throws C try, virtual, throw, final, volatile, transient D strictfp, constant, super, implements, do E byte, break, assert, switch, include
Answer: B Explanation: All the words in option B are among the 49 Java
keywords. Although goto reserved as a keyword in Java, goto is not used and has no function.
Option A is wrong because the keyword for the primitive int starts with a lowercase i.
Option C is wrong because "virtual" is a keyword in C++, but not Java.
Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final.
Option E is wrong because "include" is a keyword in C, but not in Java.
2. A _________ is a basic unit of storage in Java.
A. Identifier B. Variable C. Constant D. Memory
Answer : B Explanation
A variable is the basic unit of storage in java. All the constants of any of the data types are stored in a variable.
3. What is the default data type of a decimal number in Java?
A. int B. float C. double D. long
Answer : C
4. What would be the output of the following program?class AutoConv2 { public static void main(String args[]) { int i=257; byte b=(byte)i; System.out.println("b= "+b); }}
A. b= 1 B. b= -128 C. Compilation Error D. None of the Above
Answer : A Explanation
If the value that is being assigned (Let it be N) is larger than the range of the target integer type , the value that is actually assigned is
N%(Range of Target Datatype) In the Above Question The value assigned to "b" is 257 %
256 = 1.
5. What would be the output of the following program?class Scope1{ public static void main(String args[]) { int i=10; System.out.println(i); if (i==10) { int i=20; System.out.println(i); } System.out.println(i); }}
A. 10 20 10 B. 10 20 20 C. 10 10 10 D. Compilation Error
Answer : D Explanation
Be careful! In Java you can not define the same variabe twice.
6. What would be the output of the following program?class Scope2 { public static void main(String args[]) { int i=10; System.out.println("i="+i); if (i==10) { int j=20; i=20; System.out.println("i="+i+" , j="+ j); } System.out.println("i="+i+" , j="+ j); }}
A. i=10
i=20, j=20i=20, j=20
B. i=10i=10,j=20
C. Compilation Error D. None of the Above
Answer : C Explanation
The variable j is local to the "if" block and hence is not visible outside it.
Hence when we try to use it later while printing, the compiler reports that it is unable to find the symbol j.
7. What would be the output of the following program?class Scope3 { public static void main(String args[]) { for (int i=1;i<=3 ;i++ ) { int y=1; System.out.println("i="+i+" y="+y); y++; } }} A. i=1 y=1
i=2 y=2
i=3 y=3
B. i=1 y=1
i=2 y=1
i=3 y=1
C. i=1 y=1
i=1 y=1
i=1 y=1
D.Compilation Error
Answer : B Explanation
The declaration of variable y is inside the for loop and is executed every time the control enters the loop thus, re-initializing it to 1 everytime.
So, Y remains 1 across all the 3 iterations.
A variable declared inside a block is lost every time the control goes out of the scope thus the y declared here is created afresh and discarded at the end of every iteration.
8. Which will legally declare, construct, and initialize an array?
A int [] myList = {"1", "2", "3"}; B int [] myList = (5, 8, 2); C int myList [] [] = {4,9,7,0}; D int myList [] = {4, 3, 7};
Answer: D Explanation: The only legal array declaration and
assignment statement is Option D Option A is wrong because it initializes an int
array with String literals. Option B is wrong because it use something
other than curly braces for the initialization. Option C is wrong because it provides initial
values for only one dimension, although the declared array is a two-dimensional array.
9. Which is a reserved word in the Java programming language?
A method B native C subclasses D reference E array
Answer: B Explanation: The word "native" is a valid keyword,
used to modify a method declaration. Option A, D and E are not keywords.
Option C is wrong because the keyword for subclassing in Java is extends, not 'subclasses'.
10. Which is a valid keyword in java?
A interface B string C Float D unsigned
Answer: A Explanation: interface is a valid keyword. Option B is wrong because although
"String" is a class type in Java, "string" is not a keyword.
Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.
Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java.
11. Which one is a valid declaration of a boolean?
A boolean b1 = 0; B boolean b2 = 'false'; C boolean b3 = false; D boolean b4 = Boolean.false(); E boolean b5 = no;
Answer: C Explanation: A boolean can only be assigned the
literal true or false.
12. What is the output of the following program?class Operators1 { public static void main(String[] args) { int a=10, b=0; if( a > 10 && a/b == 10) System.out.println("Inside the If Condition!"); System.out.println("Outside the If Condition!"); }}
A. Inside the If Condtion!Outside the If Condition!
B. Outside the If Condition! C. Run Time Error D. Compile Time Error
Answer : B Explanation
Here a>10 is False and so "a/b == 10" is never executed. So no error occurs.This happens due to short circuiting of relational operators.In case of && ( Logical AND operator) exp1 && exp2Here if exp1 is False then exp2 is not evaluated as the compiler already knows that even if the exp2 is True the result will still be False i.e.False && True = False.So it does not waste time on evaluating the second expression exp2.
13. What is the output of the following program? class ModulusOperators1 { public static void main(String[] args) { float f=7.5f; System.out.println("7.5 % 2 ="+ f%2); }} A. 1 B. Compilation Error: Modulus operator can not
be applied on Floating Point Numbers C. 1.5 D. None of the Above
Answer : C Explanation
Java supports modulus operators on Floating Point Numbers too. This is opposite to C Language where you can not apply Modulus operator on Floating Point Numbers.
14. What is the output of the following program?class Operators1 { public static void main(String[] args) { int i=0; System.out.println("i--="+i--+" , --i="+ --i); }}
A. i-- = 0, --i=-2 B. i-- = -1, --i= -1 C. i-- = -1, --i =-2 D. None of the Above
Answer : A Explanation
Since, in the case of post decrement, the decremented value becomes available only in the next use, the first i-- prints the current value i.e. 0 and the value of i becomes -1, when --i is encountered later the current value of i was -1 and it is decremented again and i becomes -2 Hence -2 is printed.
15. What is the output of the following program?class ModulusOperators1 { public static void main(String[] args) { System.out.println("-8%3="+ -8%3); System.out.println("10%-3="+ 10%-3); System.out.println("-7%-3="+ -7%-3); }}
A. 2 -1 -1 B. -2 -1 1 C. -2 1 -1 D. -2 1 1
Answer : C Explanation
The sign of remainder is always same as the sign of the dividend in Java.
16. What is the output of the following program?class Operators1 { public static void main(String[] args) { int i=5,j; j=i++ + ++i; System.out.println("i="+i); System.out.println("j="+j); }} A. i=5
j=10 B. i=6
j=12 C. i=7
j=12 D. i=7
j=11
Answer : C Explanation
Since, i is incremented twice in the statement, i =7
j= 12 = 5 + 7
17. What is the output of thefollowing program?class Operators1 { public static void main(String[] args) { double x=6; int y=5,z=2; x= y++ + ++x/z; System.out.println(x); }}
A. 9 B. 8 C. 8.5 D. None of the Above
Answer : C Explanation
++ has higher precedence than division. So, ++x is executed first and then division is performed.
x= 5+ 7/2 = 5 + 3.5= 8.5
18. What is the output of the following program?class Operators1 { public static void main(String[] args) { int x =5; x *= 2 +5; System.out.println(x); }}
A.15 B.35 C.Compilation Error D.None of the Above
Answer : B Explanation
x*=2+5 => x=x*(2+5) => x=5 * (7) x=35
19. What is the output of the following program?class IfExample { public static void main(String[] args) { int var1=10; if (var1 < 10); System.out.println("Inside If Condition"); else if (var1==10) { System.out.println("Inside Else If Condition"); } else System.out.println("Inside Else Condition"); }} A. Inside Else If Condition B. Inside If Condition
Inside Else If Condition C. Compilation Error D. None of the Above
Answer : C ExplanationNotice the semi-colon at
the end of "if condition"! This ends the "empty if block" . Since the print statement is placed after it, it causes the "else if" wo be without an "if".
Note: "else if" should immediately follow either an "if" or an "else if".
20. What is the output of the following program?class SwitchControl { public static void main(String[] args) { int i=10; switch(i) { case 5 : System.out.println("Case 5!"); case 10 : System.out.println("Case 10!"); case 20 : System.out.println("Case 20!"); default : System.out.println("Default Case!"); break; } }}
A.Case 10! B.Case 5!
Case 10!Case 20!Default Case!
C.Case 10!Case 20!Default Case!
D.Compilation Error
Answer : C Explanation
This is called switch Fall-Through. All the cases after the matching case are executed in sequence until a break is found or the end of switch block is encountered.
21. What is the output of the following program?class IfExample { public static void main(String[] args) { int var1=10; if (var1 = 20) { System.out.println("Inside If Condition"); System.out.println("var1= "+var1); } }} A.Inside If Condition
var1= 20 B.Inside If Condition
var1= 10 C.Compilation Error D.Runtime Error
Answer : C Explanation
var1=20 evaluates to 20 which is an integer value , but the condition has to be a boolean value. So, we get Compilation Error.
22. What is the output of the following program?public class ForLoop{ public static void main(String[] args) { int i=0; for(;i<4;) { System.out.println("i="+i); } }} A.Compilation Error B.Run Time Error C.Infinite Loop D.None of the Above
Answer : C Explanation
If you eliminate the iteration part from the for loop, do not forget to manually increment the counter variable inside the body.
23. What is the output of the following program?public class ForLoop{ public static void main(String[] args) { int i=0; for(;i<4;) { i++; System.out.print("i="+i+” “); } }} A. i=0 i=1 i=2 i=3 B. i=1 i=2 i=3 i=4 C.Compilation Error D.None of the Above
Answer : B ExplanationIf the condition is true,
then i is first incremented and then printed, So in the first iteration when i=0, i is incremented and i=1 is printed.
So, i=1 i=2 i=3 i=4
24. What is the output of the following program?class BreakClass { public static void main(String[] args) { for(int i=0;i<5;i++) { if(i==2) { break; } System.out.print("i="+i+” “); } }} A. i=0 i=1 B. i=0 i=1 i=2 i=3 i=4 C. i=0 i=1 i=3 i=4 D.None of the Above.
Answer : A Explanation
As soon as a break is encountered the loop is terminated.
25. What is the output of the following program?public class InfiniteLoop{ public static void main(String[] args) { while(1) System.out.println("Inside Infinite Loop"); }}
A. Results in an Infinite Loop B. Compilation Error C. Inside Infinite Loop D. None of the above
Answer : B Explanation
The condition can only be a boolean expression.
26. What is the output of the following program?class BreakClass { public static void main(String[] args) { for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { if (j==1) break; System.out.println("i="+i+", j="+j); } } }}
A. i=0, j=0i=0, j=1i=1, j=0i=1, j=1
B. i=0, j=0i=1, j=0
C. i=0, j=0 D. None of the Above
Answer : B Explanation
The break inside the inner loop only interrupts the inner loop.
27. What is the output of the following program?public class ForLoop{ public static void main(String[] args) { for(int i=0;i<2;i++) { inner: for(int j=0;j<3;j++) { if (j==2) break outer; System.out.println("i="+i+", j="+j); } System.out.println("Outside the inner loop"); } outer: System.out.println("Outside the outer loop"); }}
A. i=0, j=0
i=0, j=1
i=0, j=2
i=1, j=0
i=1, j=1
i=1, j=2 B. i=0, j=0
i=0, j=1
Outside the outer loop C. Compilation Error D. None of the Above
Answer : C Explanation
A break can only be used with an enclosing label
28. What is the output of the following program?public class ForLoop{ public static void main(String[] args) { for(int i=0;i<5;) { if (i==2) continue; System.out.print("i="+i+” “); i++; } }} A. i=0 i=1 i=2 i=3 i=4 B. i=0 i=1 i=3 i=4 C.Compilation Error D.None of the Above
Answer : D Explanation Infinite Loop! The continue will skip the
i++ inside the body of the loop and thus the value of i will always remain 2 !
29. What is the output of the following program?public class ForLoop{ public static void main(String[] args) { outer: for(int i=0;i<5;i++) { inner: for(int j=0;j<5;j++) { if (j==2) break outer; System.out.println("i="+i+", j="+j); } System.out.println("Outside the inner loop"); } System.out.println("Outside the outer loop"); }}
A. i=0, j=0i=0, j=1Outside the inner loopOutside the outer loop
B. i=0, j=0i=0, j=1Outside the outer loop
C. Compilation Error D. None of the Above
Answer : B Explanation
This is a form of goto in Java using the break statement.When break is used with a label, the control comes out of the block identified by the label.
30.What is the output of the following program?class ByteSize { public static void main(String[] args) { byte b=127; System.out.println(b+1); }}
A.128 B.-127 C.Compilation Error D.None of the Above
Answer is : A Explanation
The answer is 128 add 1 to b's value i.e. 127 + 1 = 128.
31.What is the output of following code?Class Loop{
public static void main(String arg[]){
int b=0;do{
int a=2;b++;System.out.print(a++);
}while(b!=3);}
}
A. 222 B. 234 C. 345 D. Compilation error
Answer :A Explanation:
Variable a is declared every time the loop is run.
32. What is the output of the following program?Class Loop{
public static void main(String arg[]){
int i=0;for(;i<4;i++){
System.out.print(i<2+” “);}
}} A. 0 1 B. true true false false C. Compilation error D. None of these
Answer : BExplanation:
This program will compare the value of i and return true or false accordingly.
33. What is the output of the following program?Class Loop{
public static void main(String arg[]){
for(int a=0,int b=0;a+b<5;a++,b++){
System.out.print(a^b+” “); //Ex-or }
}}
A. 0 0 0 B. 1 1 1 C. 1 0 1 D. Compilation error
Answer :A
34. What is the width and range of int in java?
A. 32 bits | -2,147,483,648 to 2,147,483,647 B. 32 bits | 0 to 4294967296 C. 16 bits | -32768 to 32767 D. Compiler Dependent
Answer : A Explanation
Remember, All integers in java are signed, Java does not support unsigned int , unsigned short , unsigned byteThe rang can by found by the following Formula : -2^(n-1) to 2^(n-1) -1
35. What would be the output of the following program?class CharRange { public static void main(String[] args) { char ch=88; System.out.println(ch+" = " +(int)ch); ch=ch+1; System.out.println(ch+" = " +(int)ch); }} A.X=88
Y=99 B.X=88
X=88 C.Compiler Error D.None of the Above
Answer : C Explanation
We can not write ch= ch+1; as addition operator expects int type as its operands.We can instead write:ch++;
36. What would be the output of the following program?class OctalPrgm { public static void main(String[] args) { int i=08; System.out.println(i+1); }}
A.9 B.09 C.00 D.Compilation Error
Answer : D Explanation
The range of an octal number is from 0 to 7.
37. What would be the output of the following program?class OctalPrgm { public static void main(String[] args) { int i=07; System.out.println(++i); i=i+1; System.out.println(++i); }} A. 00
01 B. 8
9 C. 8
10 D. Compilation Error
Answer : C Explanation
i=07 since the decimal value of octal 07 is 7 , 7 is assigned to i and thus the value 8 and 10 is printed.810
Thanks