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
  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
  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 {
  int b = 1;
  public static void main(String [] args) {
       System.out.println("b is " + b);
 }
}
  1. Compilation error

  2. Runtime Error

  3. Runtime Exception

  4. Output of b is 1


Correct Option: A

AI Explanation

To answer this question, you need to understand the concept of static and instance variables in Java.

In the given code, the variable b is an instance variable of the class A because it is declared within the class but outside any method or constructor.

The main method in Java is a static method, which means it belongs to the class itself and not to any specific instance of the class. Static methods can only access static variables directly. In this case, the main method is trying to access the instance variable b directly, which is not allowed.

Therefore, the given code will result in a compilation error.

The correct answer is A) Compilation error.

  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
  1. 4 bits

  2. 7 bits

  3. 8 bits

  4. 16 bits


Correct Option: D