162). What is the stored in the object obj in following lines of Java code?
box obj;
[A]Memory address of allocated memory of object
[B]NULL
[C] Any arbitrary pointer
[D]Garbage
Show Answer
Correct Answer: NULL
Notes:
Answer: b
Explanation: Memory is allocated to an object using new operator. box obj; just declares a reference to object, no memory is allocated to it hence it points to NULL.
163). Which of these keywords is used to make a class?
box obj;
[A] class
[B]struct
[C]int
[D] none of the mentioned
Show Answer
Correct Answer: class
Notes:
Answer: a
Explanation: None.
164). Which of the following is a valid declaration of an object of class Box?
box obj;
[A]Box obj = new Box();
[B] Box obj = new Box;
[C] obj = new Box();
[D] new Box obj;
Show Answer
Correct Answer: Box obj = new Box();
Notes:
Answer: a
Explanation: None.
165). Which of these operators is used to allocate memory for an object?
box obj;
[A]malloc
[B]alloc
[C]new
[D]give
Show Answer
Correct Answer: new
Notes:
Answer: c
Explanation: Operator new dynamically allocates memory for an object and returns a reference to it. This reference is address in memory of the object allocated by new.
166). Which of these statement is incorrect?
box obj;
[A] Every class must contain a main() method
[B]Applets do not require a main() method at all
[C]There can be only one main() method in a program
[D] main() method must be made public
Show Answer
Correct Answer: Every class must contain a main() method
Notes:
Answer: a
Explanation: Every class does not need to have a main() method, there can be only one main() method which is made public.
167). What will be the output of the following Java program?
class main_class
{
public static void main(String args[])
{
int x = 9;
if (x == 9)
{
int x = 8;
System.out.println(x);
}
}
}
[A]9
[B]8
[C]Compilation error
[D] Runtime error
Show Answer
Correct Answer: Compilation error
Notes:
Answer: c
Explanation: Two variables with the same name can’t be created in a class.
output:
advertisement
$ javac main_class.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable x
168). Which of the following statements is correct?
class main_class
{
public static void main(String args[])
{
int x = 9;
if (x == 9)
{
int x = 8;
System.out.println(x);
}
}
}
[A] Public method is accessible to all other classes in the hierarchy
[B] Public method is accessible only to subclasses of its parent class
[C]Public method can only be called by object of its class
[D]Public method can be accessed by calling object of the public class
Show Answer
Correct Answer: Public method is accessible to all other classes in the hierarchy
Notes:
Answer: a
Explanation: None.
8. What will be the outpu
169). What will be the output of the following Java program?
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
obj.width = 10;
obj.height = 2;
obj.length = 10;
int y = obj.width * obj.height * obj.length;
System.out.print(y);
}
}
[A]12
[B]200
[C]400
[D]100
Show Answer
Correct Answer: 200
Notes:
Answer: b
Explanation: None.
output:
$ javac mainclass.java
$ java mainclass
200
170). What will be the output of the following Java program?
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();
obj1.height = 1;
obj1.length = 2;
obj1.width = 1;
obj2 = obj1;
System.out.println(obj2.height);
}
}
[A]1
[B]2
[C]Runtime error
[D] Garbage value
Show Answer
Correct Answer: 1
Notes:
Answer: a
Explanation: When we assign an object to another object of same type, all the elements of right side object gets copied to object on left side of equal to, =, operator.
output:
$ javac mainclass.java
$ java mainclass
1
171). What will be the output of the following Java program?
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
System.out.println(obj);
}
}
[A]0
[B]1
[C]Runtime error
[D] classname@hashcode in hexadecimal form
Show Answer
Correct Answer: classname@hashcode in hexadecimal form
Notes:
Answer: d
Explanation: When we print object internally toString() will be called to return string into this format classname@hashcode in hexadecimal form.
output:
$ javac mainclass.java
$ java mainclass
box@130671e