Olete.in
Articles
Mock Tests
🧪 Java MCQ Quiz Hub
Java Mcq Question Set 3
Choose a topic to test your knowledge and improve your Java skills
1. Output : public class Test{ public static void main(String args[]){ String str1 = "one";String str2 = "two" ; System.out.println(str1.concat(str2)); } }
one
two
onetwo
twoone
2. The class string belongs to ................. package.
java.lang
java.awt
java.string
java.applet
3. Output : public class Test{ public static void main (String[] args){ String test = "a1b2c3"; String[] tokens = test.split("\d"); for(String s: tokens) system.out.print(s); } }
123
321
abc
cba
4. How many objects will be created? String a = new String("Examveda"); String b = new String("Examveda"); String c = "Examveda"; String d = "Examveda";
1
2
3
None
5. How many Constructor String class have?
11
12
13
14
6. What will be output? String S1 = "S1 ="+ "123"+"456"; String S2 = "S2 ="+(123+456);
S1=579,S2=579
S1=123456,S2=123456
S1=12345, S2=57
S1=123456, S2=579
7. int x = 0, y = 0 , z = 0 ; x = (++x + y-- ) * z++; What will be the value of "x" after execution ?
0
1
-1
2
8. int ++a = 100 ; System.out.println( ++a ) ; What will be the output of the above fraction of code ?
100
Displays error as ++a is not enclosed in double quotes in println statement
Compiler displays error as ++a is not a valid identifier
None of these
9. Output : class Numbers{ public static void main(String args[]){ int a=20, b=10; if((a < b) && (b++ < 25)){ System.out.println("This is any language logic"); } System.out.println(b); } }
10
11
12
13
10. Select from among the following character escape code which is not available in Java.
a
\
11. What will be the output? if(1 + 1 + 1 + 1 + 1 == 5){ System.out.print("TRUE"); } else{ System.out.print("FLASE"); }
TRUE
FALSE
Compiler Error
None of these
12. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?
(1 &gt; x &gt; 100) || (x &lt; 0)
1 &lt; x &lt; 100 || x &lt; 0
((x &lt; 100) &amp;&amp; (x &gt; 1)) || (x &lt; 0)
((x &lt; 100) &amp;&amp; (x &gt; 1)) &amp;&amp; (x &lt; 0)
13. Output : public class Test { public static void main(String args[]){ int a = 42; double b = 42.25; System.out.print((a)+" "+(b)); } }
2 2.5
4.2 4.225
2 4.225
4.2 4.225
14. Output : public class Test{ public static void main(String... args){int x =5; x *= 3 + 7; System.out.println(x); } }
10
25
35
50
15. Output : public class Test{ public static void main(String... args){ int a=5 , b=6, c=7; System.out.println("Value is "+ b + c); System.out.println(a + b + c); System.out.println("String " + (b+c)); }
Value is 13 18 String
Value is 67 18 String 13
Value is 13 18 String 13
Compilation fails
16. What will be the return type of a method that not returns any value?
void
int
double
None of the above
17. Which of the following options is the best for generating random integer 0 or 1?
(int)Math.random()
(int)Math.random() + 1
(int)(Math.random() + 0.2)
(int)(Math.random() + 0.5)
18. What is Math.floor(3.6)?
3
4
3
4
19. In which area of memory, the system stores parameters and local variables whenever a method is invoked?
Stack
Array
Heap
Storage Area
20. The variables declared in a class for the use of all methods of the class are called
instance variables
reference variables
objects
None of these
21. Output : public class Test{ public static void main(String args[]){System.out.println( Math.floor( Math.random( ) ) ) ; } }
0
0.5
1
10
22. Output : class Num { Num(double x ){ System.out.println( x ) ; } } public class Test extends Num {public static void main(String[] args){ Num num = new Num( 2 ) ;} }
0
1
Compile time error
None of the above
23. The implicit return type of a constructor is
void
There is no return type
A class object in which it is defined.
None of the above
24. The finalize() method is called just prior to
An object, variable or method goes out of scope.
An object or variable goes out of scope.
A variable goes out of scope.
Before garbage collection.
25. The main method should be static for the reason
It can be accessed easily by the class loader.
It can be executed without creating any instance of the class.
It can be accessed by every method or variable without any hindrance.
None of the above
26. public class Test { } What is the prototype of the default constructor?
public Test( )
public Test(void)
Test(void)
Test( )
27. public class MyClass{ } For the above class(MyClass) what is the correct way of declaring constructor?
MyClass(){}
public MyClass(){}
1 and 3
None of these
28. Output : public class Test{public static void main(String[] args){String value = "abc"; changeValue(value); System.out.println(value); }public static void changeValue(String a){a = "xyz"; }}
xyz
abc
Compilation fails
Compilation clean but no output
29. Which of these is a legal definition of a method named gkindiaonline assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select the one correct
gkindiaonline() throw IOException{}
void gkindiaonline() throw IOException{}
void gkindiaonline() throws IOException{}
void gkindiaonline(void) throws IOException{}
30. Output : public class Test{public static void main(String args[]){int i; for(i = 1; i < 6; i++){if(i > 3) continue ; } System.out.println(i); } }
5
6
7
8
31. In java, ............ can only test for equality, where as .......... can evaluate any type of the Boolean expression.
if, switch
if, break
switch, if
continue, if
32. Output : public class Test{public static void main(String args[]){int i = 0, j = 5 ; for( ; (i < 3) && (j++ < 10) ; i++ ){System.out.print(" " + i + " " + j ); }System.out.print(" " + i + " " + j ); }
0 6 1 7 2 8 3 8
0 6 1 7 2 8 3 9
0 6 1 5 2 5 3 5
Compilation Error
33. Output : class Test {public static void main(String args[]){int x=7; if(x==2); // Note the semicolon System.out.println("NumberSeven");System.out.println("NotSeven"); } }
NotSeven
NumberSeven
NumberSeven NotSevev
None of these
34. Output : public class Test{public static void main(String args[]){ int i, j; for(i=1, j=0;i<10;i++) j += i; System.out.println(i); } }
8
9
10
11
35. Output : public class Test{ public static void main(String[] args){ int x = 3, y = 4; switch(x + 3){ case 6: y = 0; case 7: y = 1; default: y += 1; } } }
0
1
2
3
36. How many times will the following code print "Welcome to Gkindiaonline"? int count = 0; do {System.out.println("Welcome to Gkindiaonline"); count++; } while (count < 10);
10
11
12
13
37. Choose the correct statement in context of the following program code. public class Test{public static void main(String[] args){double sum = 0; for(double d = 0; d < 10;){d += 0.1; sum += sum + d; }}
The program runs in an infinite loop because d&lt;10 would always be true.
The program has a compile error because the adjustment is missing in the for loop.
The program has a compile error because the control variable in the for loop cannot be of the double type.
The program compiles and runs fine.
38. Which of the following for loops will be an infinite loop?
for(i=0 ; i&lt;1; i--)
for(i=0; ; i++)
for(; ;)
All of the above
39. Output : public class Test{static public void main(String args[]){ //line 2 int i, j; for(i=0; i<3; i++){for(j=1; j<4; j++){ i%=j; System.out.println(j); } } }
Repeatedly print 1 2 3 and cause infinite loop.
1 2 3 1
1 2 3 2
None of these
40. What is the value of a[1] after the following code is executed? int[] a = {0, 2, 4, 1, 3}; for(int i = 0; i < a.length; i++) a[i] = a[(a[i] + 3) % a.length];
0
1
2
3
41. Output : public class Test {public static void main(String... args) throws Exception{Integer i = 34; int l = 34;if(i.equals(l)){System.out.println("true"); }else{System.out.println("false"); } } }
TRUE
FALSE
Compiler error
None of these
42. Output : public class Test{ public static void main(String args[]){ int i = 1; do{ i--; }while(i > 2); System.out.println(i); }
0
1
2
3
43. Output : int i = 10; while(i++ <= 10){ i++; } System.out.print(i);
11
12
13
14
44. ___________ method cannot be overridden.
static
final
super
private
45. Given the following piece of code: public class School{ public abstract double numberOfStudent(); } which of the following statements is true?
You must add a return statement in method numberOfStudent().
The keywords public and abstract cannot be used together.
The method numberOfStudent() in class School must have a body.
Class School must be defined abstract.
46. Which of the following class definitions defines a legal abstract class?
abstract class A { abstract void unfinished(); }
abstract class A { abstract void unfinished(); }
class A { abstract void unfinished(); }
class A { abstract void unfinished() { } }
Submit