Java Exam Preparation Pactice Test - MCQ
Java Exam Preparation Pactice Test - MCQ
1.
3.
class Q006{
public static void main(String args[]){
boolean flag = false;
if ( flag = true) {
import java.io.*;
class Q032{
public static void main(String args[]) throws Exception{
FileInputStream fin;
int c = 0;
try {
fin = new FileInputStream(args[0]);
while ((c = fin.read()) != -1) {
System.out.print((char)c);
}
} catch (Exception e) {
System.out.println(e);
}
fin.close();
}
}
System.out.println(true);
}
if ( flag == false) {
System.out.println(false);
}
}
}
Options :
a. true
b. false
c. Compile-time error " Q006.java: Incompatible type
for declaration. Can't convert boolean to
java.lang.Boolean. "
d. true
false
Options :
a. Compile-time error " Variable fin may not have been
initialized."
b. Run-time exception "
java.lang.ArrayIndexOutOfBoundsException: 0"
c. Run's fine displaying the contents of the file .
d. Compile-time error " Undefined variable: args"
2.
4.
class A{
static int i;
A(){
++i;
}
private int get(){
return ++i;
}
}
class B extends A{
B(){
i++;
}
int get(){
return ( i + 3);
}
}
Options :
a. 123
b. 23
c. 3
d. 1 , 2 , 3 each on a separate line
6.
Options :
a. 1
b. -1
c. Compile error
d. Run-time error
7.
8.
10.
class Q009{
public static void main(String args[]){
System.out.println(
(Math.abs(Integer.MIN_VALUE)));
//For Your Information,
//Integer.MIN_VALUE = -2147483648
}
}
class Test{
static boolean flag;
public static void main(String args[]){
if (flag) {
System.out.println(flag);
}
}
}
Options :
a. 2147483648
b. Compile Error
c. -2147483648
d. NegativeArraySizeException thrown at runtime
Options :
a. Compiler error, boolean flag: variable flag may not
have been initialized
b. Compiler error, System.out.println (flag): can't
convert boolean to String
c. true
d. false
e. Compiles & runs fine with no output generated
11.
}
class B extends A {
B() {
//some initialization
}
void do_something() {
System.out.println("I'm in B");
}
}
class Test01{
public static void main(String args[]){
A a = new B();
a.do_something();
}
}
Options :
a. 0
b. 10
c. null
d. Compile error "Q012.java:10: No variable i defined
in class Q012. "
9.
Options :
a. I'm in A
b. I'm in B
c. Compiler error , A a = new B ( ) : Explicit cast
needed to convert A to B
d. ClassCastException thrown at runtime
Options :
a. 10
10
b. Compile error " Q012.java:16: Variable i may not
have been initialized. "
c. 0
10
d. 10
0
13.
16.
class Q018 {
static Object obj;
static String get() {
return null;
}
}
}
Options :
a. Compile error
b. Run-time error
c. true
d. false
14.
Options :
a. null
b. Compiler error , can't invoke a method on null
c. NullPointerException thrown at runtime
d. ClassCastException thrown at runtime
class Q015{
public String get(int i){
return null;
}
class Q022 {
public static void main(String ka[]) {
while(false) {
System.out.println("Great");
}
}
}
Options :
a. Run's with no output
b. Run-time error "Statement not reached."
c. Great
d. Compiler error, System.out.println ( "Great"):
statement not reached
}
Options :
a. null
b. NullPointerException at run-time
c. Compile error
d. 0
15.
18.
class Q013{
int i;
Q013(int i){
System.out.println(i);
}
}
class B extends A{
private int get(){
return 2;
}
}
class Q027 extends B{
public static void main(String ka[]) {
Q027 obj = new Q027();
System.out.println(obj.get());
}
}
Options :
a. 2
b. Compile error , System.out.println (obj.get ( )): no
method matching get( ) found in class Q027
c. 1
d. 2
e. NullPointerException thrown at run-time .
19.
23.
24.
class Q029{
public static void main(String args[]){
boolean b[] = new boolean[2];
System.out.println(b[1]);
}
}
Options :
a. Compile error " Variable b may not have been
initialized."
b. Run-time error " Variable b may not have been
initialized."
c. false
d. true
21.
class Q009{
public static void main(String args[]) {
float f = 1/2;
System.out.println(f);
}
}
Options :
a. Compile-time error float f = 1/2 : explicit cast
needed to convert int to float
b. 0.0
c. 0.5
d. ClassCastException thrown at run-time
Options :
a. Compile-time error "Explicit cast needed to
convert int to char"
b. The code can be made to compile by
commenting out lines marked as 1 & 2
c. The code can be made to compile by
commenting out the line "char a = 'a' + 2;"
d. The output is:
67
67
22.
25.
29.
Options :
a. A
b. 65
c. Compile time error, case 65: duplicate case label
d. Runtime error "Duplicate case label: 65"
e. default
27.
33.
class Evolve {
static int i = 1;
static int j = 2;
int x = 3;
static int y = 6;
class Test02{
public static void main(String args[]){
String str = args[0];
switch ( str.equals("just")) {
case 1:
System.out.println("case 1");
break;
case 2:
System.out.println("case 2");
break;
default:
break;
}
}
}
32.
Options :
a. Output = case 1
b. Output = case 2
c. No output generated
d. Compiler error, switch(str.equals("just")) : Can't
convert boolean to int.
e. Compiler error, case 1 : can't convert boolean to int
34.
37.
Options :
a. case 1
case2
default
b. case 1
c. Runtime exception thrown
-NumberFormatException,
Integer.parseInt(args[0] ): Can't convert String to int
d. The code won't compile because there is no method
parseInt( ) defined in the Integer class .
35.
40.
class Test04{
public static void main(String args[]){
for ( int i = 1 ; i <= 5 ; i++ ) {
if ( i < 5 ) {
continue;
} else System.out.println( i );
}
}
}
class Test09 {
static int tomb(char a) {
throw new NumberFormatException();
}
public static void main(String args[]) {
try {
tomb('a');
} catch (Exception e) {
System.out.println("Done");
}
}
Options :
a. 1 to 5 are printed on a new line each
b. 1 to 4 are printed on a new line each
c. Only 5 is printed
d. 2 to 5 are printed on a new line each
e. 2 to 4 are printed on a new line each
f. No output is generated because continue terminates
the loop
36.
}
Options:
a. NumberFormatException thrown at runtime
b. Compiler error , throw new
NumberFormatException() : Checked Exceptions
must be declared in the throws clause or must be
caught in the corresponding catch block
c. Compiler error, System.out.println("Done") :
statement not reached
d. Output = Done
38.
41.
42.
Answers:
1. a
2. b
3. a
4. a
5. c
6. a
7. c
8. d
9. a
10. e
11. b
12. d
13. c
14. b
15. d
16. a
17. d
18. b
19. c
20. e
21. a & b
22. c
23. b
24. b
25. c
26. b
27. d
28. e
29. c
30. c , d & e
31. d
32. d
33. d
34. a
35. c
36. e
37. d
38. e
39. c
40. d
41. b
42. c