Core Java Hardest Test: Trivia Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Jeevabharathi
J
Jeevabharathi
Community Contributor
Quizzes Created: 1 | Total Attempts: 145
Questions: 40 | Attempts: 145

SettingsSettingsSettings
Core Java Hardest Test: Trivia Quiz! - Quiz


Do you know the core Java language? Java is a classed based language that was made to have minimum implementation dependencies. It is a general-purpose programming language that allows application developers to run anywhere. Java works on different platforms such as Windows, Mac, Linux, and Raspberry Pi. It is relatively easy to learn and simple to use. This quiz is the most challenging core Java quiz, but you can do it. Go for it.


Questions and Answers
  • 1. 

    Choose the possible constructor arguments of the Float wrapper class.

    • A.

      Float

    • B.

      Double

    • C.

      String

    • D.

      Int

    • E.

      Byte

    Correct Answer(s)
    A. Float
    B. Double
    C. String
    Explanation
    The possible constructor arguments for the Float wrapper class are float, double, and string. The Float class provides constructors that accept these data types as parameters to create Float objects. The float and double arguments can be used to directly initialize a Float object with their respective values. The string argument can be used to create a Float object by parsing the string representation of a floating-point number. The int and byte arguments are not valid constructor arguments for the Float wrapper class.

    Rate this question:

  • 2. 

    Integer y = 567;   Integer x = y;   System.out.println(y==x);  y++; System.out.println(x + " " + y); System.out.println(y==x);

    • A.

      False 568 567 true

    • B.

      True 567 566 false

    • C.

      True 567 568 false

    • D.

      False 567 568 true

    Correct Answer
    C. True 567 568 false
    Explanation
    The code first assigns the value of y (567) to x. Then, it prints the result of the comparison y==x, which is true because both variables have the same value. Next, the value of y is incremented by 1. After that, it prints the values of x and y, which are 567 and 568 respectively. Finally, it prints the result of the comparison y==x, which is false because the values of y and x are no longer the same after the increment.

    Rate this question:

  • 3. 

    Class Boxing2 { static Integer x; public static void main(String [] args) { doStuff(x); } static void doStuff(int z) { int z2 = 5; System.out.println(z2 + z); } } From the above code, Will it throws a NullPointerException?

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The code will throw a NullPointerException because the variable "x" in the "doStuff" method is not initialized and has a default value of null. When the "doStuff" method is called in the main method, it passes the value of "x" (which is null) as an argument. Therefore, when trying to print the sum of "z2" and "z", a NullPointerException will occur.

    Rate this question:

  • 4. 

    Class EasyOver { static void go(int x) { System.out.print("int "); } static void go(long x) { System.out.print("long "); } static void go(double x) { System.out.print("double "); } public static void main(String [] args) { byte b = 5; short s = 5; long l = 5; float f = 5.0f; go(b); go(s); go(l); go(f); } } Predict the output of the above code.  

    • A.

      Int double long int

    • B.

      Long int double int

    • C.

      Double int int long

    • D.

      Int int long double

    Correct Answer
    D. Int int long double
    Explanation
    The output of the code will be "int int long double". This is because the method overloading in the EasyOver class determines the appropriate method to call based on the type of the argument. In this case, the arguments passed to the go() method are of type byte, short, long, and float. The byte and short arguments will be automatically promoted to int, so the go(int x) method will be called. The long argument will match the go(long x) method, and the float argument will match the go(double x) method. Therefore, the output will be "int int long double".

    Rate this question:

  • 5. 

    Class AddBoxing { static void go(Integer x) { System.out.println("Integer"); } static void go(long x) { System.out.println("long"); } public static void main(String [] args) { int i = 5; go(i);  } } Which go() will be invoked? (choose 1)  

    • A.

      Long and Integer

    • B.

      Integer

    • C.

      Long

    • D.

      None of the above

    Correct Answer
    C. Long
    Explanation
    The go() method that will be invoked is the one with the parameter type long. This is because the argument passed to go() is of type int, and since there is no exact match for int, the compiler will look for the closest match, which is long.

    Rate this question:

  • 6. 

    Class BoxAndWiden { static void go(Object o) { Byte b2 = (Byte) o; System.out.println(b2); } public static void main(String [] args) { byte b = 5; go(b); } } What will be the output of above program?  

    • A.

      0

    • B.

      5

    • C.

      Returns garbage value

    • D.

      Compiler error

    Correct Answer
    B. 5
    Explanation
    The program will output 5. The method "go" takes an Object as a parameter, and the byte variable "b" is passed as an argument to this method. Inside the method, the Object is casted to a Byte and assigned to the variable "b2". Since the value of "b" is 5, it can be successfully casted to a Byte. Therefore, when "b2" is printed, it will output 5.

    Rate this question:

  • 7. 

     public class Test {   public static void main(String[] args) { Integer in = 1;   int i = 1;  System.out.print(in.equals(i));   System.out.print(in == i);  }} What will be the result of above code ?

    • A.

      False false

    • B.

      True true

    • C.

      False true

    • D.

      True false

    Correct Answer
    B. True true
    Explanation
    The code compares an Integer object (in) with an int primitive (i). The equals() method in the Integer class compares the values of the objects, so in.equals(i) returns true. However, the == operator compares the references of the objects, so in == i returns false. Therefore, the result of the code will be true false.

    Rate this question:

  • 8. 

     public class Test {     public static void main(String[] args) {     Number i=10;     int j = 10;     if (i.equals(j))     {     System.out.println("equals");     }     else   System.out.println("not equals"); } } what will be the result of above code?  

    • A.

      Not equal

    • B.

      Equal

    • C.

      Compile time error at line 3

    • D.

      Compile time error at line 5

    Correct Answer
    B. Equal
    Explanation
    The code will output "equals". This is because the Number class overrides the equals() method to compare the values of the objects. In this case, the value of the Number object i is 10, and the value of the int variable j is also 10. Therefore, the condition in the if statement is true and the code inside the if block is executed, printing "equals" to the console.

    Rate this question:

  • 9. 

    Examine: 1   public class Test3 {   2    static void pass(int i) {     }   3    static void pass(Integer i) { 4    System.out.println(i);     }   5    public static void main(String[] args) { 6    pass(1);     }     } What will be the result of above code ?  

    • A.

      1

    • B.

      Compile time error at line no 2.

    • C.

      Print nothing

    • D.

      Compile time error at line no 6.

    Correct Answer
    C. Print nothing
    Explanation
    The code will print nothing because the method pass(int i) is not called in the main method. Instead, the method pass(Integer i) is called with the argument 1, which is autoboxed to an Integer object. However, this method does not contain any code to print the value of the Integer object. Therefore, the code will not produce any output.

    Rate this question:

  • 10. 

    1    public class Test2 { 2    public static void main(String[] args) { 3    int i = 1; 4    Integer I = new Integer(i); 5    method(i); 6    method(I); 7    }   8    static void method(Integer I) { 9    System.out.print(" Wrapper"); 10   }   11    static void method(int i) { 12    System.out.print(" Primitive"); }} What will be the result of above code ?  

    • A.

      Primitive Wrapper

    • B.

      Wrapper

    • C.

      Primitive

    • D.

      Wrapper Primitive

    Correct Answer
    A. Primitive Wrapper
    Explanation
    The code will print "Primitive Wrapper" as the output. This is because the method with the Integer parameter will be called when the argument is of type Integer, and the method with the int parameter will be called when the argument is of type int. Since the argument in the main method is of type int, it will be autoboxed into an Integer object and the method with the Integer parameter will be called. The method with the Integer parameter will print "Wrapper" and the method with the int parameter will print "Primitive".

    Rate this question:

  • 11. 

    1. public class Test {  2. public static void main(String [ ] args) {  3. 4. switch(a) {  5. default:  6. System.out.println("You choose correct option");  7. } } } Which given below snippet is best for inserting at line number 3 to compile the above code successfully. (choose 3)  

    • A.

      Int a = 3;

    • B.

      Short a = 4;

    • C.

      Char a = 'p';

    • D.

      Long a= 388;

    • E.

      Boolean a = true;

    Correct Answer(s)
    A. Int a = 3;
    B. Short a = 4;
    C. Char a = 'p';
  • 12. 

    1 .public class LoopOutput{ 2. public static void main(String args[]) { 3. int a = 4; 4. int b = 3; 5. int c = 2; 6. for(;b < 7;b++) { 7. if(c++ > a) { 8. a = b + c; 9. } 11. } 12. System.out.println("Value of C is " + c); 13. } 14. } What is the output of variable "c" after completion of for loop :

    • A.

      1

    • B.

      4

    • C.

      6

    • D.

      8

    Correct Answer
    C. 6
    Explanation
    The value of variable "c" is initially 2. Inside the for loop, the condition "c++ > a" is checked. Since "c" is initially less than "a", the condition is not satisfied and the code inside the if statement is not executed. Therefore, the value of "c" remains unchanged. The for loop continues until "b" is less than 7, incrementing "b" by 1 each time. After the loop completes, the value of "c" is still 2.

    Rate this question:

  • 13. 

    1 .public class Question3{ 2 .public static void main(String[] args) { 3 .try { 4 .int a= (int)(Math.random()*5); 5 .if(a<=2.5) 6 .System.out.println("a="+a); 7 .else 8 .throw new Exception("a>2.5"); 9. } catch (Exception e){   10. System.err.println(e.getMessage() ); 11. System.err.println("Value of a="+a); 12. } } } Find the output of the following code :  

    • A.

      An Exception is raised during compilation

    • B.

      An Exception is thrown at run time

    • C.

      Unresolved compilation problem

    • D.

      A=2 or a=1 or i >2.5 Value of i =null

    Correct Answer
    C. Unresolved compilation problem
    Explanation
    The given code has a compilation error because the variable "a" is declared and used inside the try block, but it is also being used outside the try block in the catch block. This causes an "Unresolved compilation problem" because the catch block cannot access the variable "a" which is declared inside the try block.

    Rate this question:

  • 14. 

    1.  import java.io.*; 2.  public class Example4 { 3.  public static void main(String[] args) { 4.  Example4 e4 = new Example4(); 5.  try{ 6.  e4.check();} 7.  catch(IOException e){}} 8.  void check() throws IOException{ 9.  System.out.println("Inside check() method of Class "); 10. throw new IOException();}} 11. class Subexample4 extends Example4 { 12. void check() { 13. System.out.println("Inside check() Method of Subclass"); 14. }} What will be the output of the following code snippet  :  

    • A.

      Above program will throw exception & will not run

    • B.

      Inside check() method of class

    • C.

      Inside check() method of Subclass

    Correct Answer
    B. Inside check() method of class
    Explanation
    The correct answer is "Inside check() method of class". This is because the code snippet is calling the check() method from the Example4 class, which is the superclass. The check() method in the Example4 class is throwing an IOException, so the catch block in the main method will catch the exception. Since there is no catch block in the check() method of the Subexample4 subclass, the check() method of the superclass will be executed. Therefore, the output will be "Inside check() method of class".

    Rate this question:

  • 15. 

    What should be at line number 3 to get the total sum of array "sum" ? public int totalsum( int[] sum ){ int a, b= 0 ; //which 'for' loop should be here(line :3)  {  b += sum[ a++ ] ; } return b ; } Which 'for' loop should be at line number 3 to calculate total sum of the array "sum" :

    • A.

      for( int a = 0 ; a< sum.length ; )

    • B.

      For( a= 0 ; a< sum.length ; )

    • C.

      For( a = 0 ; a< sum.length ; a++ )

    • D.

      for( a = 1 ; i

    Correct Answer
    B. For( a= 0 ; a< sum.length ; )
    Explanation
    The correct 'for' loop to calculate the total sum of the array "sum" is "for( a= 0 ; a< sum.length ; )". This loop initializes the variable 'a' to 0, continues the loop as long as 'a' is less than the length of the array "sum", and does not have an increment statement. Inside the loop, the value at index 'a' in the array "sum" is added to the variable 'b' using the shorthand operator '+='. This loop will iterate through all the elements of the array and accumulate their sum in the variable 'b'.

    Rate this question:

  • 16. 

    Which modifier should  used in place of  **** , so that it's subclass saved in other package can access " FixVariable() " method of it's super class ?   package NewPackage; public class Example extends rose{ private int length; *** void FixVariable( int a ) { length = a; } }     // subclass in different package     import NewPackage.Example ; public class SubExample extends Example { SubExample() { FixVariable( 201 ); }

    • A.

      Private and protected

    • B.

      Public and protected

    • C.

      Protected

    • D.

      Default(blank)

    Correct Answer
    B. Public and protected
    Explanation
    The "public" modifier allows the method to be accessed by any class, including classes in other packages. The "protected" modifier allows the method to be accessed by subclasses, even if they are in different packages. Therefore, using both "public" and "protected" modifiers will allow the subclass in the other package to access the "FixVariable()" method of its superclass.

    Rate this question:

  • 17. 

    Public class Example6{ public static void main(String args[]) { public int check(char a) { if (a <= 'N') { if (a == 'E') return 2; return 1; } else if (a == 'S') return 3; else if (a == 'W') return 4; return 0; } } } Which are correct input-output options : (choose 2)

    • A.

      A='A' output=1.

    • B.

      Compile error.

    • C.

      A='X' output=0.

    • D.

      A='D' output=0.

    Correct Answer(s)
    A. A='A' output=1.
    C. A='X' output=0.
    Explanation
    The given code is a Java program that contains a method called "check" which takes a character as input and returns an integer based on certain conditions. The code checks if the input character is less than or equal to 'N'. If it is, then it checks if the character is 'E' and returns 2 if true, otherwise it returns 1. If the input character is not less than or equal to 'N', then it checks if the character is 'S' and returns 3 if true, else it checks if the character is 'W' and returns 4 if true. If none of these conditions are satisfied, it returns 0. Therefore, for the given options, the correct input-output combinations are a='A' output=1 and a='X' output=0.

    Rate this question:

  • 18. 

    What is the output of the following 'for' loop code :   public class Example12 { public static void main(String[] args) { int a, b; flag: for (a = 0; a < 6; a++) { for (b = 5; b > 2; b--) { if (a == b) { System.out.print(" " + b); continue flag; } } }   } }}  

    • A.

      5

    • B.

      Compile error

    • C.

      5 4

    • D.

      5 4 3

    Correct Answer
    D. 5 4 3
    Explanation
    The output of the code is "5 4 3". The outer loop iterates from 0 to 5 and the inner loop iterates from 5 to 2. When a and b are equal, the code prints the value of b and continues to the next iteration of the outer loop using the "continue" statement with the label "flag". This causes the inner loop to skip the remaining iterations and start again from the initial value of b. This process continues until the outer loop finishes. As a result, the values of b that are printed are 5, 4, and 3.

    Rate this question:

  • 19. 

    What would be the output after executing following code :   public class Example13 { public static void main(String[] args) throws Exception { int a = 30; while (a) { System.out.print("value:" + a--); } } }  

    • A.

      Compile error

    • B.

      30 29,.....untill 1

    • C.

      1 2 3.....untill 30

    • D.

      No output

    Correct Answer
    A. Compile error
    Explanation
    The code will result in a compile error because the condition in the while loop expects a boolean value, but the variable 'a' is an integer.

    Rate this question:

  • 20. 

    Find the Output of the following code :   public class Example15 { public static void main(String[] args) { for (int a = 0; a < 10; ++a) { try { if (a % 3 == 0) throw new Exception("Except1"); try { if (a % 3 == 1) throw new Exception("Except2"); System.out.println(a); } catch (Exception inside) { a *= 2; } finally { ++a; } } catch (Exception outside) { a += 3; } finally { ++a; } } } }  

    • A.

      5 8

    • B.

      5

    • C.

      8

    • D.

      Compile error

    Correct Answer
    A. 5 8
    Explanation
    The code uses nested try-catch-finally blocks to handle exceptions. In the outer try block, if the value of 'a' is divisible by 3, it throws an exception "Except1". In the inner try block, if the value of 'a' modulo 3 is 1, it throws an exception "Except2". If an exception is caught inside the inner catch block, the value of 'a' is multiplied by 2. In the outer catch block, if an exception is caught, the value of 'a' is incremented by 3. In the finally block, the value of 'a' is always incremented by 1.

    For the given code, the output will be 5 8. This is because when 'a' is 5, it is not divisible by 3, so no exception is thrown. Hence, it is printed. When 'a' is 6, it is divisible by 3, so "Except1" is thrown and caught in the outer catch block, incrementing 'a' by 3. Hence, 'a' becomes 9. Finally, 'a' is incremented by 1 in the outer finally block, making it 10. Since 'a' is now greater than 10, the loop terminates.

    Rate this question:

  • 21. 

    Given a sample code:   1 public class Sample { 2 public static void main(String args[]) {  3 int k=1, m=3; 4 do { 5 k=++k; 6 m--; 7 } while(m>0); 8 System.out.println(k); 9 }}   What will be the result of above code ?  

    • A.

      1

    • B.

      2

    • C.

      4

    • D.

      The program compilation fails because of line number 5.

    Correct Answer
    C. 4
    Explanation
    The code starts with initializing variables k and m to 1 and 3 respectively. Then, it enters a do-while loop. In each iteration of the loop, the value of k is incremented by 1 and the value of m is decremented by 1. This continues until m becomes 0. Since the loop executes 3 times (m starts at 3 and decrements to 0), the value of k becomes 4. Finally, the value of k is printed, which is 4. Therefore, the result of the code is 4.

    Rate this question:

  • 22. 

    Given a sample code:   1    public class Sample{ 2    public static void main(String args[]) { 3    int i = 1, j = 2; 4    i++; 5    j--; 6    if (i / j > 1) 7    i++; 8    System.out.println(i + j); }}   What will be the result of above code ?  

    • A.

      1

    • B.

      2

    • C.

      4

    • D.

      The program compilation fails because of line number 6.

    Correct Answer
    C. 4
    Explanation
    The result of the above code will be 4. This is because the code first increments the value of i by 1 (i++), and then decrements the value of j by 1 (j--). After that, it checks if the value of i divided by j is greater than 1. Since i is 2 and j is 1, the condition is true and the code increments the value of i by 1 (i++). Finally, it prints the sum of i and j, which is 4.

    Rate this question:

  • 23. 

    Given a sample code: 1    public class Sample { 2    public static void main(String args[]) { 3    int i = 1, j = 2; 4    i++; 5    j--; 6    if (i % j > 1) 7    i++; 8    System.out.println("i = "+i+ " j = "+j); }} What will be the result of above code ?

    • A.

      i = 2 j = 1

    • B.

      i = 1 j = 2

    • C.

      i = 2 j = 2

    • D.

      The program compilation fails because of line number 6.

    Correct Answer
    A. i = 2 j = 1
    Explanation
    The code initializes two variables, i with a value of 1 and j with a value of 2. Then, it increments i by 1 and decrements j by 1. In the if statement, it checks if the remainder of i divided by j is greater than 1. Since i is 2 and j is 1, the remainder is 0, which is not greater than 1. Therefore, the if statement is false and the program moves on to the next line. Finally, it prints the values of i and j, which are 2 and 1 respectively.

    Rate this question:

  • 24. 

    Given a sample code: 1    public class Test { 2    public static void main(String args[]) {  3    int i = 5; 4    do { 5    i--; 6    } while (i > 2); 7    System.out.println(i); 8    }} What will be the result of above code ?

    • A.

      1

    • B.

      2

    • C.

      4

    • D.

      The program compilation fails because of line number 6.

    Correct Answer
    B. 2
    Explanation
    The code starts with the variable `i` being assigned a value of 5. Then, it enters a do-while loop where `i` is decremented by 1 each time. The loop continues as long as `i` is greater than 2. Since `i` starts at 5 and is decremented by 1 each time, it will go through the loop 3 times (5, 4, 3) until `i` becomes 2. Once `i` becomes 2, the condition of the loop is no longer true, so the loop ends. After the loop, the value of `i` is printed, which is 2. Therefore, the result of the code will be 2.

    Rate this question:

  • 25. 

    Given a sample code: 1    public class Test { 2    public static void main(String args[]) {  3    int j; 4    do { 5    j++; 6       } 7    while(j < 0); 8    System.out.println(j); }} What will be the result of above code ?

    • A.

      The program compilation fails because of line number 3.

    • B.

      1

    • C.

      3

    • D.

      The program compilation fails because of line number 5.

    Correct Answer
    D. The program compilation fails because of line number 5.
    Explanation
    The program compilation fails because of line number 5. This is because the variable "j" is not initialized before it is used in the do-while loop. The variable "j" is declared on line 3, but it is not given an initial value. Therefore, when it is incremented on line 5, it does not have a defined value. To fix this, the variable "j" should be initialized to a specific value before the do-while loop.

    Rate this question:

  • 26. 

    Given a sample code: 1    public class Sample { 2    static int j=5; 3    public static void main(String args[]) { 4    while (j > 0) { 5    j--; 6    } 7    System.out.println(j); }} What will be the result of above code ?

    • A.

      1

    • B.

      2

    • C.

      0

    • D.

      4

    Correct Answer
    C. 0
    Explanation
    The result of the above code will be 0. This is because the code initializes the variable "j" with a value of 5 and then enters a while loop. In each iteration of the loop, the value of "j" is decremented by 1. The loop continues until "j" becomes 0. After the loop, the value of "j" is printed, which is 0.

    Rate this question:

  • 27. 

    Given a sample code: 1    public class Test { 2    public static void main(String args[]) { 3    int i, j, k; 4    for (i = 0; i < 4; i++) { 5    for (j = 1; j < 3; j++) { 6    for (k = 2; k < 3; k++) { 7    if ((i == j) && (j != k)) 8    System.out.println(i);        }}} }} What will be the result of above code ?

    • A.

      1

    • B.

      0

    • C.

      4

    • D.

      The program compilation fails because of line number 2.

    Correct Answer
    A. 1
    Explanation
    The code will print the number 1. This is because the nested for loops iterate through the values of i, j, and k. The if statement checks if i is equal to j and if j is not equal to k. When i is equal to j and j is not equal to k, the code prints the value of i, which is 1.

    Rate this question:

  • 28. 

    Given a sample code: 1    public class Test { 2    public static void main(String args[]) { 3    int i, j ; 4    for (i = 0; i < 3; i++) { 5    for (j = 1; j < 4; j++) { 6    i %= j;  7    System.out.print(j); }} }} What will be the result of above code ?

    • A.

      1 2 3

    • B.

      1 1 2

    • C.

      Repeatedly print 123 and cause infinite loop.

    • D.

      The program compilation fails because of line number 2.

    Correct Answer
    C. Repeatedly print 123 and cause infinite loop.
    Explanation
    The given code will repeatedly print the numbers 1, 2, and 3 and cause an infinite loop. This is because the inner loop (line 5-7) will execute three times for each iteration of the outer loop (line 4-7). In each iteration of the inner loop, the value of 'i' is being updated using the modulus operator (%=) with the value of 'j'. Since 'j' starts from 1 and increases by 1 in each iteration, 'i' will always be 0 after the first iteration of the inner loop. Therefore, the condition 'i < 3' (line 4) will always be true, resulting in an infinite loop.

    Rate this question:

  • 29. 

    Given a sample code: public class Test { public static void main(String args[]) { int i = 9; while (i++ <= 12) { i++; } System.out.print(i); } } What will be the result of above code ?

    • A.

      10

    • B.

      14

    • C.

      13

    • D.

      11

    Correct Answer
    B. 14
    Explanation
    The code starts with i = 9 and enters a while loop. In each iteration of the loop, i is incremented by 1 twice (i++ in the condition and i++ inside the loop). Since the condition is i++

    Rate this question:

  • 30. 

    Given a sample code: 1    public class Test2 { 2    public static void main(String[] args) { 3    int i = 1; 4    for (; i <= 10; i++) { 5    if (i > 7) 6    break;     } 7    System.out.println(i); }} What will be result when compiled and run?

    • A.

      7

    • B.

      8

    • C.

      8 9 10

    • D.

      Compilation error at line no-4.

    Correct Answer
    B. 8
    Explanation
    The code will output 8. The for loop starts with i=1 and continues until i

    Rate this question:

  • 31. 

    Given a sample code: 1    public class Test { 2    public static void main(String[] args) { 3    int i = 100;  4    switch(i)  5    {  6    default:  7    System.out.println("Default");  8    case 100:  9    System.out.println("100");  10  case 200:  11  System.out.println("200"); }  }} What will be result when compiled and run?

    • A.

      100 200

    • B.

      Compilation error at line no-6.

    • C.

      Compilation error at line no-9.

    • D.

      Compilation error at line no-11.

    Correct Answer
    A. 100 200
    Explanation
    The code will compile and run without any errors. The output will be "100 200". This is because the variable "i" is initialized with the value 100 and the switch statement checks the value of "i". Since "i" is equal to 100, it will execute the code block for case 100 and print "100". After that, it will continue to execute the code block for case 200 and print "200".

    Rate this question:

  • 32. 

    Given a sample code: 1    public class Test2 { 2    public static void main(String[] args) { 3    int i = 3; 4    System.out.println("start"); 5    switch (i) { 6    case 3: 7    continue; 8    case 4:     } 9    System.out.println("end");     }} What will be result when compiled and run?

    • A.

      Start

    • B.

      End

    • C.

      Start end

    • D.

      Compilation error at line no-7.

    Correct Answer
    D. Compilation error at line no-7.
    Explanation
    The code will result in a compilation error at line number 7 because the use of the "continue" statement outside of a loop is not allowed in Java. The "continue" statement is used to skip the rest of the current iteration of a loop and move to the next iteration. Since there is no loop in the code, the "continue" statement is invalid and causes a compilation error.

    Rate this question:

  • 33. 

    Given a sample code: 1    public class Test2 { 2    public static void main(String[] args) { 3    System.out.println("Hello"); 4    break; 5    System.out.println("Hi"); }} What will be result when compiled and run?

    • A.

      Hello

    • B.

      Hello Hi

    • C.

      Runtime exception.

    • D.

      Compilation error at line no-4.

    Correct Answer
    D. Compilation error at line no-4.
    Explanation
    The code will result in a compilation error at line no-4 because the "break" statement is used outside of a loop or switch statement, which is not allowed. The "break" statement is used to exit a loop or switch statement prematurely, but in this case, there is no loop or switch statement present.

    Rate this question:

  • 34. 

    Wrapper classes will widen from one to another. Eg: class Dog4 { public static void main(String [] args) { Dog4 d = new Dog4(); d.test(new Integer(5));  } void test(Long x) { } } Say true or false  

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The given code will result in a compilation error because there is no method in the Dog4 class that accepts an Integer parameter. The test() method only accepts a Long parameter. Therefore, the code will fail to compile and the answer is false.

    Rate this question:

  • 35. 

    int x = 3; if (x = 5) { } what will be the expected result?

    • A.

      It compiles and runs fine

    • B.

      It shows compile error

    • C.

      On run time it will show error

    • D.

      Nothing will happen

    Correct Answer
    B. It shows compile error
    Explanation
    The code will show a compile error because the condition in the if statement is using the assignment operator (=) instead of the equality operator (==). The assignment operator is used to assign a value to a variable, while the equality operator is used to compare two values. In this case, the code is trying to assign the value 5 to the variable x, which is not allowed in an if statement.

    Rate this question:

  • 36. 

    int trueInt = 1; int falseInt = 0; if (trueInt) if (trueInt == true) if (1)  if (falseInt == false) if (trueInt == 1) if (falseInt == 0) which are all illegal substitutions? (choose 4)

    • A.

      If (trueInt)

    • B.

      If (trueInt == true)

    • C.

      If (1)

    • D.

      If (falseInt == false)

    • E.

      If (trueInt == 1)

    • F.

      If (falseInt == 0)

    Correct Answer(s)
    A. If (trueInt)
    B. If (trueInt == true)
    C. If (1)
    D. If (falseInt == false)
    Explanation
    The given substitutions are all illegal because they violate the syntax rules of the programming language. In the first substitution, "if (trueInt)" is illegal because the condition in an if statement must be a boolean expression, not just a variable. The second substitution, "if (trueInt == true)", is also illegal because the "true" in this context is not a variable or a valid boolean expression. The third substitution, "if (1)", is illegal because the condition must be a boolean expression, not just a number. The fourth substitution, "if (falseInt == false)", is illegal because the "false" in this context is not a variable or a valid boolean expression.

    Rate this question:

  • 37. 

    boolean boo = false; if (boo = true) { } Will if test succeeds?  

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The if statement in the code is checking the value of the variable "boo". In the given code, the variable "boo" is being assigned the value of true using the assignment operator (=). Therefore, the condition in the if statement will evaluate to true, and the if test will succeed.

    Rate this question:

  • 38. 

    What will happen if a continue statement is outside a loop.

    • A.

      Runs fine

    • B.

      Shows compile time error

    • C.

      Shows run time error

    • D.

      None of the above

    Correct Answer
    B. Shows compile time error
    Explanation
    If a continue statement is outside a loop, it will result in a compile time error. The continue statement is used to skip the current iteration of a loop and move to the next iteration. However, if the continue statement is placed outside of any loop, there is no loop to continue from, resulting in a syntax error during compilation.

    Rate this question:

  • 39. 

    int x = 1; while (x) { }  while (x = 5) { }  while (x == 5) { } while (true) { } Which are all illegal while expressions?(choose 2)  

    • A.

      While (x) { }

    • B.

      While (x = 5) { }

    • C.

      While (x == 5) { }

    • D.

      While (true) { }

    Correct Answer(s)
    C. While (x == 5) { }
    D. While (true) { }
    Explanation
    The first three while expressions are legal because they have a valid condition in the parentheses. The last while expression is also legal because "true" is a boolean value that will always evaluate to true. Therefore, the two illegal while expressions are "while (x == 5) { }" and "while (true) { }" because they have valid conditions but do not terminate.

    Rate this question:

  • 40. 

    int x; long x2; Long [] La = {4L, 5L, 6L}; long [] la = {7L, 8L, 9L}; int [][] twoDee = {{1,2,3}, {4,5,6}, {7,8,9}}; String [] sNums = {"one", "two", "three"}; Animal [] animals = {new Dog(), new Cat()}; for(x2 : la) ; for(int x2 : twoDee) ; for(int x3 : la) ; for(Dog d : animals) ; which are all legal 'for' declarations?

    • A.

      For(x2 : la) ;

    • B.

      For(int x2 : twoDee) ;

    • C.

      For(int x3 : la) ;

    • D.

      For(Dog d : animals) ;

    • E.

      None of the above

    Correct Answer
    E. None of the above

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 15, 2011
    Quiz Created by
    Jeevabharathi
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.