Tag: java

Questions Related to java

  1. Compilation and running with output 0 to 9

  2. Run time error: Constructors cannot be declared protected

  3. Compilation error: Constructors cannot be declared protected

  4. Compilation and running with output 0 to 10

  5. None of the above


Correct Option: A

What is output of the above program?

Class conditional {
 public static void main(String args[]) {
  int i = 20;
  int j = 55;
  int z = 0;
  z = i < j ? i : j; // ternary operator
  System.out.println("The value assigned is " + z);
 }
}
  1. 20

  2. 55

  3. None of the above


Correct Option: A

AI Explanation

To answer this question, let's go through the code step by step:

The given code snippet demonstrates the usage of the ternary operator to assign a value to the variable z based on a condition.

The ternary operator (condition) ? (expression1) : (expression2) works as follows:

  • If the condition is true, it evaluates to expression1.
  • If the condition is false, it evaluates to expression2.

In this case, the condition is i < j, where i is 20 and j is 55.

Since the condition i < j is true (20 is less than 55), the ternary operator will evaluate to i, which is 20.

Therefore, the value assigned to z is 20.

The output of the program will be: "The value assigned is 20"

Hence, the correct answer is option A) 20.

  1. Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type

  2. The size of a string can be retrieved using the length property.

  3. Strings are a primitive type in Java that overloads the + operator for concatenation

  4. The String class is implemented as a char array, elements are addressed using the stringname[] convention


Correct Option: C

A class cannot be declared:

  1. Static

  2. Private

  3. Default

  4. None of the above


Correct Option: B
  1. Compilation error

  2. Runtime error

  3. No errors

  4. Execptions


Correct Option: C

Following code will result in:

class A { 
   public static void main(String [] args) {
       B b = new A(); 
   }
} 
class B extends A {}
  1. Compilation error

  2. Runtime Error

  3. Runtime Exception

  4. Other Execptions


Correct Option: A

AI Explanation

To answer this question, let's go through each option:

Option A) Compilation error - This option is correct. The code will result in a compilation error. The reason is that in the main method, an instance of class A is being assigned to a variable of type B. However, class A is not a subclass of class B, so this assignment is not allowed. This will result in a compilation error.

Option B) Runtime Error - This option is incorrect. A runtime error occurs when the code executes and encounters an error that prevents it from continuing. In this case, the code will not reach the runtime stage because it fails to compile.

Option C) Runtime Exception - This option is incorrect. A runtime exception occurs when an exception is thrown during the execution of the code. In this case, the code will not reach the runtime stage because it fails to compile.

Option D) Other Exceptions - This option is incorrect. Since the code fails to compile, it will not reach the stage where other exceptions can occur.

The correct answer is A) Compilation error. This option is correct because the code fails to compile due to the mismatch in the assignment of an instance of class A to a variable of type B.

What is an instance of

  1. A methods in object

  2. An operator and keyword

  3. Both

  4. None


Correct Option: B

What happens when you do if (a==b)?

Integer a = new Integer(2);
 Integer b = new Integer(2); 
  1. Compiler error

  2. Runtime Exception

  3. FALSE


Correct Option: C

What is the size of a Char?

  1. 4 bits

  2. 7 bits

  3. 8 bits

  4. 16 bits


Correct Option: D