Core Java Hardest Test: Trivia Quiz!

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Jeevabharathi
J
Jeevabharathi
Community Contributor
Quizzes Created: 1 | Total Attempts: 148
| Attempts: 148 | Questions: 40
Please wait...
Question 1 / 40
0 %
0/100
Score 0/100
1. Integer y = 567;   Integer x = y;   System.out.println(y==x);  y++; System.out.println(x + " " + y); System.out.println(y==x);

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.

Submit
Please wait...
About This Quiz
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... see moreallows 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. see less

2. 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 ?

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.

Submit
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?

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.

Submit
4. 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 ?  

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.

Submit
5. 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?

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".

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

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.

Submit
7. 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.  

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".

Submit
8. 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 ?  

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".

Submit
9. 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 ); }

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.

Submit
10. 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 ?  

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.

Submit
11. 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 ?

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.

Submit
12. 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 ?

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.

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

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.

Submit
14.  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 ?

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.

Submit
15. 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 ?

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.

Submit
16.  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?  

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.

Submit
17. 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?

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.

Submit
18. 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 :

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.

Submit
19. 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  

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.

Submit
20. 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 ?

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

Submit
21. 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?  

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.

Submit
22. 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--); } } }  

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.

Submit
23. 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?

Explanation

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

Submit
24. 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 ?  

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.

Submit
25. 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" :

Explanation

The correct 'for' loop to calculate the total sum of the array "sum" is "for( a= 0 ; a

Submit
26. 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)

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.

Submit
27. 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  :  

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".

Submit
28. 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 ?

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++

Submit
29. 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)

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.

Submit
30. 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; } } }   } }}  

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.

Submit
31. Choose the possible constructor arguments of the Float wrapper class.

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.

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

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.

Submit
33. 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?

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.

Submit
34. 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 ?

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.

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

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.

Submit
36. 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 :  

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.

Submit
37. 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; } } } }  

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.

Submit
38. 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)  

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.

Submit
39. 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)  

Explanation

not-available-via-ai

Submit
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?

Explanation

not-available-via-ai

Submit
View My Results

Quiz Review Timeline (Updated): Mar 21, 2023 +

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
Cancel
  • All
    All (40)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Integer y = 567;...
Given a sample code: ...
Class Boxing2 {...
Given a sample code:...
Given a sample code: ...
Boolean boo = false;...
Class EasyOver {...
1    public class Test2 {...
Which modifier should  used in place of  **** , so that...
Given a sample code:...
Given a sample code: ...
Given a sample code: ...
What will happen if a continue statement is outside a loop.
 public class Test {...
Given a sample code: ...
 public class Test {...
Given a sample code: ...
1 .public class LoopOutput{...
Wrapper classes will widen from one to another....
Given a sample code: ...
Class BoxAndWiden {...
What would be the output after executing following code :...
Given a sample code: ...
Examine:...
What should be at line number 3 to get the total sum of array...
Int trueInt = 1;...
1.  import java.io.*;...
Given a sample code: ...
Public class Example6{...
What is the output of the following 'for' loop code :...
Choose the possible constructor arguments of the Float wrapper class.
Int x = 1;...
Given a sample code: ...
Given a sample code: ...
Int x = 3; if (x = 5) { } what will be the expected result?
1 .public class Question3{...
Find the Output of the following code :...
Class AddBoxing {...
1. public class Test { ...
Int x;...
Alert!

Advertisement