Java MCQs on class fundamentals & object declaration in Java Programming Language.
1. What is the stored in the object obj in following lines of Java code?
a) Memory address of allocated memory of object Answer: b 2. Which of these keywords is used to make a class? Answer: a 3. Which of the following is a valid declaration of an object of class Box? Answer: a 4. Which of these operators is used to allocate memory for an object? Answer: c 5. Which of these statement is incorrect? Answer: a 6. What will be the output of the following Java program? a) 9 7. Which of the following statements is correct? Answer: a 8. What will be the output of the following Java program? a) 12 9. What will be the output of the following Java program? a) 1 10. What will be the output of the following Java program? a) 0
b) NULL
c) Any arbitrary pointer
d) Garbage
Clarification: 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.
a) class
b) struct
c) int
d) none of the mentioned
Clarification: None.
a) Box obj = new Box();
b) Box obj = new Box;
c) obj = new Box();
d) new Box obj;
Clarification: None.
a) malloc
b) alloc
c) new
d) give
Clarification: 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.
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
Clarification: Every class does not need to have a main() method, there can be only one main() method which is made public.
class main_class
{
public static void main(String args[])
{
int x = 9;
if (x == 9)
{
int x = 8;
System.out.println(x);
}
}
}
b) 8
c) Compilation error
d) Runtime error
Clarification: Two variables with the same name can’t be created in a class.
output:
$ javac main_class.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable 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
Clarification: None.
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);
}
}
b) 200
c) 400
d) 100
Clarification: None.
output:
$ javac mainclass.java
$ java mainclass
200
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);
}
}
b) 2
c) Runtime error
d) Garbage value
Clarification: 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
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
System.out.println(obj);
}
}
b) 1
c) Runtime error
d) [email protected] in hexadecimal form
Clarification: When we print object internally toString() will be called to return string into this format [email protected] in hexadecimal form.
output: