4 Multiple Choices of Java

Java – Static Analysis

What is the output of the following Java code?

public class Test {
    public static void main(String[] args){
        int i = 010;
        int j = 07;
        System.out.println(i);
        System.out.println(j);
    }
}
Pick ONE option
8 7
10 7
Compilation fails with an error at line 3
Compilation fails with an error at line 5
 

The output of the given Java code is:

10
7

The code int i = 010; declares an integer variable i and initializes it with the value 010. In Java, if an integer literal starts with a leading zero, it is treated as an octal (base 8) value. Therefore, 010 is interpreted as the octal value 8 in decimal format.

The code int j = 07; declares an integer variable j and initializes it with the value 07. Here, 07 is a valid octal value, and its decimal equivalent is 7.

When we print the values of i and j using System.out.println(), the output will be:

10
7

So, the correct option is: 10 7

Will This Compile?

 Given the follow code, which are true? (Choose all that apply):

1. interface Syrupable { 
2. void getSugary(); 
3. }
4. abstract class Pancake implements Syrupable { } 
5. 
6. class BlueBerryPancake implements Pancake { 
7.     public void getSugary() { ; } 
8. } 
9. class SourdoughBlueBerryPancake extends BlueBerryPancake { 
10.     void getSugary(int s) { ; } 
11. }
Pick ONE option
Compilation succeeds.
Compilation fails due to an error on line 2.
Compilation fails due to an error on line 4.
Compilation fails due to an error on line 6.
Compilation fails due to an error on line 7.
Compilation fails due to an error on line 9.
Compilation fails due to an error on line 10.

The correct option is:

Compilation fails due to an error on line 10.

The reason for this is that line 10 in the code attempts to define a method getSugary(int s) in the class SourdoughBlueBerryPancake, but it does not override the method getSugary() from the Pancake interface or provide an implementation for it.

To successfully override a method, the method signature (including the name, return type, and parameters) must exactly match the method being overridden. In this case, the method getSugary(int s) in SourdoughBlueBerryPancake does not match the method getSugary() in the Syrupable interface or the inherited method from Pancake, resulting in a compilation error.

The other lines of code do not have compilation errors because they correctly implement or extend the necessary interfaces and classes.

Static Code Analysis

What will the output be?

try 
{
    Float f1 = new Float("3.0");
    int x = f1.intValue();
    byte b = f1.byteValue();
    double d = f1.doubleValue();
    System.out.println(x + b + d);
}
catch (NumberFormatException e) /* Line 9 */
{
    System.out.println("bad number"); /* Line 11 */
}
Pick ONE option
9.0
bad number
Compilation fails on line 9
Compilation fails on line 11

The output of the given code will be:

9.0

The code attempts to perform arithmetic operations on the variables xb, and d and then prints the result using System.out.println().

Let’s break down the code:

  1. Float f1 = new Float("3.0"); creates a Float object with the value 3.0.
  2. int x = f1.intValue(); extracts the integer value from the Float object and assigns it to the variable x. In this case, the integer value is 3.
  3. byte b = f1.byteValue(); extracts the byte value from the Float object and assigns it to the variable b. In this case, the byte value is 3.
  4. double d = f1.doubleValue(); extracts the double value from the Float object and assigns it to the variable d. In this case, the double value is 3.0.
  5. System.out.println(x + b + d); performs the addition of xb, and d and prints the result. The result is 9.0 because x and b are both 3 (an integer and a byte have the same value in this case), and d is 3.0.

Since there is no NumberFormatException thrown in the code, the catch block on line 9 is not executed. Therefore, the output will not be “bad number”.

The correct option is: 9.0

Which of the following is not a correct way of commenting?

Which of the following is not a correct way of commenting?

Pick ONE option
/* Here is a comment **** */
/* This is also a comment /* More comments */ */
/* This is also a comment // More comments */
// /* This is a // // comment */

The option that is not a correct way of commenting is:

/* This is also a comment // More comments */

In Java, the /* */ syntax is used for multiline comments, and // is used for single-line comments.

In the given option, the comment starts with /*, but it contains // within it. Nested comments are not allowed in Java. Therefore, the code /* This is also a comment // More comments */ will result in a compilation error.

The correct way to comment out code in Java would be to use either /* */ for multiline comments or // for single-line comments.