Java Programming Test! Hardest Trivia Questions 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 Catherine Halcomb
Catherine Halcomb
Community Contributor
Quizzes Created: 1443 | Total Attempts: 6,714,021
| Attempts: 162 | Questions: 5
Please wait...
Question 1 / 5
0 %
0/100
Score 0/100
1. 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?

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

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

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

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.

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

Explanation

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

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

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.

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

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.

Submit
View My Results

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

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
Cancel
  • All
    All (5)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Given: ...
Given the code fragment: ...
Given:...
Given:...
Given the code fragment: ...
Alert!

Advertisement