Java Data Types

Question 1
class Main {   
   public static void main(String args[]) {      
         int t;      
         System.out.println(t); 
    }   
}
Cross
0
Cross
garbage value
Tick
compiler error
Cross
runtime error


Question 1-Explanation: 
Unlike class members, local variables of methods must be assigned a value to before they are accessed, or it is a compile error.
Question 2
Predict the output of following Java program.
class Test { 
    public static void main(String[] args) { 
      for(int i = 0; 0; i++) 
      { 
          System.out.println("Hello"); 
          break; 
      } 
    } 
} 
Cross
Hello
Cross
Empty Output
Tick
Compiler error
Cross
Runtime error


Question 2-Explanation: 
The error is in for loop where 0 is used in place of boolean value. Unlike C++, use of non boolean variables in place of bool is not allowed
Question 3
Predict the output of the following program.
 class Test
{
    public static void main(String[] args)
    {
        Double object = new Double("2.4");
        int a = object.intValue();
        byte b = object.byteValue();
        float d = object.floatValue();
        double c = object.doubleValue();

        System.out.println(a + b + c + d );

    }
}
Cross
8
Cross
8.8
Tick
8.800000095367432


Question 3-Explanation: 
Arithmetic conversions are implicitly performed to cast the values to a common type. The compiler first performs integer promotion. If the operands still have different types, then they are converted to the type that appears highest in the hierarchy.
Question 4
Which of the following statements is/are TRUE regarding JAVA ? (a) Constants that cannot be changed are declared using the ‘static’ keyword. (b) A class can only inherit one class but can implement multiple interfaces.
Cross
Only (a) is TRUE.
Tick
Only (b) is TRUE.
Cross
Both (a) and (b) are TRUE.
Cross
Neither (a) nor (b) are TRUE.


Question 4-Explanation: 
In JAVA, constant are not declared using 'static' keyword and a class can implement multiple interfaces but class can inherit one class only. So, option (B) is correct.
There are 4 questions to complete.

  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads