Java Programming Test! Hardest Trivia Questions 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 Catherine Halcomb
Catherine Halcomb
Community Contributor
Quizzes Created: 1385 | Total Attempts: 6,256,328
Questions: 5 | Attempts: 161

SettingsSettingsSettings
Java Programming Test! Hardest Trivia Questions Quiz - Quiz

Below is what is seen by many as the hardest trivia questions java programming test. It is not designed for beginners when it comes to this course but for the professionals who think they understand this programming language and how to use it. How about you give it a shot and get to see if you are a novice or an accrual pro when it comes to all things java.


Questions and Answers
  • 1. 

    Given: public class Test{ public static void main(String[] args) { Test obj = new Test(); short letter = 97; int letter2 = 98; int long = 99; System.out.print((char) letter + " "); System.out.print((char) letter2); } } What is the result?

    • A.

      A b

    • B.

      A followed by an exception

    • C.

      Compilation fails.

    • D.

      A ClassCastException is thrown at runtime.

    Correct Answer
    C. Compilation fails.
    Explanation
    The code fails to compile because "long" is a reserved keyword in Java and cannot be used as a variable name.

    Rate this question:

  • 2. 

    Given the code fragment:  public static void main(String[] args) { short s1 = 200; int s2 = 400; long s3 = (long) s1 + s2; // line n1 String s4 = (String) (s3 * s2 ); // line n2 System.out.println("Sum is " + s4); } What is the result?

    • A.

      Sum is 600

    • B.

      Compilation fails at line n1.

    • C.

      Compilation fails at line n2.

    • D.

      A exception is thrown at line n1.

    • E.

      A exception is thrown at line n2.

    Correct Answer
    C. Compilation fails at line n2.
    Explanation
    The code will fail to compile at line n2 because it is attempting to cast a long value to a String, which is not allowed.

    Rate this question:

  • 3. 

    Given: public class Test{ public static void main(String[] args) { short letter; int letter2 = letter + 2; // line n1 System.out.print((short) letter2); // line n2 } } What is the result?

    • A.

      Compilation fails at line n1.

    • B.

      Compilation fails at line n2.

    • C.

      A exception is thrown at line n1.

    • D.

      A exception is thrown at line n2.

    Correct Answer
    A. Compilation fails at line n1.
    Explanation
    The compilation fails at line n1 because the variable "letter" is declared but not initialized before it is used in the expression "letter + 2". Since the value of "letter" is not known at this point, the compiler cannot perform the addition and assign the result to "letter2". To fix this error, the variable "letter" should be assigned a value before line n1.

    Rate this question:

  • 4. 

    Given the code fragment: public static void main(String[] args) { int a = 10; float b = 10.25f; double c = 100; a = b; // line 7 b = a; // line 8 c = b; // line 9 c = a; // line 10 } Which change enables the code fragment to compile successfully?

    • A.

      Replace line 10 with c = (double) a;

    • B.

      Replace line 8 with b = (float) a;

    • C.

      Replace line 7 with a = (int) b;

    • D.

      Replace line 9 with c = (double) b;

    Correct Answer
    C. Replace line 7 with a = (int) b;
    Explanation
    The code fragment does not compile successfully because there are incompatible data types being assigned to variables. In order to fix this, we need to explicitly cast the incompatible data types to compatible ones. In this case, replacing line 7 with "a = (int) b;" will cast the float variable b to an int, allowing it to be assigned to the int variable a.

    Rate this question:

  • 5. 

    Given: public class Test{ public static void main(String[] args) { Test obj = new Test(); short letter = 97; int letter2 = 98; System.out.print((char) letter + " "); System.out.print((char) letter2); } } What is the result?

    • A.

      A b

    • B.

      A followed by an exception

    • C.

      Compilation fails

    • D.

      A ClassCastException is thrown at runtime.

    Correct Answer
    A. A b
    Explanation
    The code first creates an instance of the Test class. It then assigns the value 97 to a variable named "letter" of type short, and assigns the value 98 to a variable named "letter2" of type int.

    In the first print statement, the value of "letter" is casted to a char, which corresponds to the ASCII value of 'a'. The second print statement directly prints the char value of "letter2", which corresponds to the ASCII value of 'b'.

    Therefore, the result of the code is "a b".

    Rate this question:

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 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jul 07, 2018
    Quiz Created by
    Catherine Halcomb
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.