Test Series - java

Test Number 48/64

Q: Java object assignment happens by ___.
A. Pass by Value
B. Pass by Reference
C. Compiler error
D. None of the above
Solution: Yes. That is the reason why you can change the values of variables of the object using another reference.
Q: Java object passing from one method to another method happens by ___.
A. Pass by Value
B. Pass by Reference
C. Compiler error
D. None of the above
Solution: References point to the original objects. So they can change the state of the objects.
Q: In Java Pass by reference ___ is passed even if you are passing a reference to an object.
A. Address value
B. Variable value
C. Hash code
D. None of the above
Solution: Yes. The address is passed automatically by Java. So, Java pundits argue that it is passing a value (Address).
Q: A Java reference is comparable to ___ in C language.
A. Enum
B. Structure
C. Pointer
D. None
Solution: no solution
Q:  ___ is the superclass to all Java classes either user-defined or built-in.
A. Class
B. Object
C. Superclass
D. Null
Solution: Yes. java.lang.Object is the superclass to all Java classes.
Q: State TRUE of FALSE. Java objects have built-in methods to handle threads.
A. TRUE
B.  FALSE
C. Compiler error
D. None of the above
Solution: Yes. The methods are wait(), notify() and notifyAll().
Q: What is the output of the below Java program using toString() method?
class College
{
  public String toString()
  { return "College Object"; }
}
class Testing18
{
  public static void main(String[] args)
  {
    College col = new College();
    System.out.println("Printing Object=" + col);
  }
}
A. Printing Object=
B. Printing Object=null
C. Printing Object=College Object
D. Compiler error
Solution: print() and println() methods call toString() method of objects automatically.
Q: What is the output of the below Java program?
class Cricket
{ int runs; }

class Testing19
{
  public static void main(String[] args)
  {
    Cricket c1 = new Cricket();
    c1.runs = 250;
    Cricket c2;
    c2 = c1;
    c2.runs = 300;
    System.out.println("Runs= " + c1.runs);
  }
}
A. Runs= 0
B. Runs= 250
C. Runs= 300
D. Compiler error
Solution: The reference C2 also points to the same object pointed by reference C1.
Q: What is the output of the below Java program?
class Wordpress
{ int posts; }
class Testing20
{
  public static void main(String[] args)
  {
    Wordpress wp1 = new Wordpress();
    wp1.posts = 25;
    Wordpress wp2 = wp1;
    wp1 = null;
    System.out.println("Posts=" + wp2.posts);
  }
}
A. Posts=25
B. Posts=0
C. Posts=null
D. Runtime exception occurs
Solution: Even if one REFERENCE to the same object is alive, it can be used to access the object. So, wp2 still works even if wp1 is set to null.

You Have Score    /9