Test Series - java

Test Number 50/64

Q: What is the output of the below Java program with a void method?
public class TestingMethods3
{
  void show2()
  {
    System.out.println("SHOW Method 2");
  }
  public static void main(String[] args)
  {
    TestingMethods3 t3 = new TestingMethods3();
    t3.show2();
  }
}
A. SHOW Method 2
B. No output
C. Compiler error
D. None
Solution: The empty return statement is not necessary for a void method.
Q: A "this" operator used inside a Java method refers to ___ variable.
A. Global variable
B. Method local variable
C. Instance variable
D. None
Solution: no solution
Q: What is the output of the below Java program with a "this" operator?
public class TestingMethods4
{
  int cakes=5;
  void order(int cakes)
  {
    this.cakes = cakes;
  }
  public static void main(String[] args)
  {
    TestingMethods4 t4 = new TestingMethods4();
    t4.order(10);
    System.out.println("CAKES=" + t4.cakes);
  }
}
A. CAKES=5
B. CAKES=0
C. CAKES=10
D. Compiler error
Solution: In the program, this.cakes refers to the instance variable cakes.
Q: A local variable declared inside a method can not be used in expressions without initializing it first. (TRUE / FALSE).
A. TRUE
B. FALSE
C. None
D. Compiler error
Solution: no solution
Q: What is the output of the below Java program?
public class TestingMethods5
{
  public static void main(String[] args)
  {
    int localVariable;
    System.out.println(localVariable);
  }
}
A. 0
B. garbage value
C. NullPointerException
D. Compiler error
Solution: In the above program, the localVariable is a Local variable and it is not initialized. You can not use it in any expressions, not even printing.
Q: In Java, local variables are stored in __ memory and instance variables are stored in ___ memory.
A. Stack, Stack
B. Heap, Heap
C. Stack, Heap
D. Heap, Stack
Solution: no solution
Q: A static-method or a static-variable is shared among all instances of a class. (TRUE / FALSE)
A. TRUE
B.  FALSE
C. None
D. Compiler error
Solution: Yes. a single copy of a static variable or method is common to all instance objects.
Q: What is the output of the Java program with static variables?
public class TestingMethods6
{
  static int cats=25;
  public static void main(String[] args)
  {
    TestingMethods6 t6 = new TestingMethods6();
    System.out.println("t6 BIRDS before=" + t6.cats);
    TestingMethods6 t7 = new TestingMethods6();
    t7.cats = 10;
    System.out.println("t6 BIRDS after=" + t6.cats);
  }
}
A. t6 BIRDS before=25 t6 BIRDS after=25
B. t6 BIRDS before=25 t6 BIRDS after=10
C. t6 BIRDS before=25 t6 BIRDS after=0
D.  None
Solution: The static variable "cats" is common to all objects. There is no separate copy like non-static variables.
Q: What is the output of the below Java program with a final local variable?
public class TestingMethods8
{
  int cars = 20;
  void change(final int cars)
  {
    cars = 10;
    this.cars = cars;
  }
  public static void main(String[] args)
  {
    TestingMethods8 t8 = new TestingMethods8();
    t8.change(30);
    System.out.println(t8.cars);
  }
}
A. 30
B. 20
C. 10
D. Compiler error
Solution: The argument that is marked final can not be reassigned or changed. So, the compiler error is produced. So, the statement cars=10; inside the change() method is wrong.
Q: Java does not allow nesting of methods. (TRUE / FALSE)
A. TRUE
B. FALSE
C. None
D. Compiler error
Solution: no solution
Q: What is the output of the below Java program?
class Road
{
  static void show()
  {
    System.out.println("Inside static method.");
  }
}

public class TestingMethods10
{
  public static void main(String[] args)
  {
    Road.show();
  }
}
A. Inside static method.
B. empty message
C. Compiler error
D. Runtime error / exception
Solution: You can directly call static methods of a class with just a DOT operator and class-name.

You Have Score    /11