Java Questions & Answers – Class Fundamentals & Declaring objects

This section of our 1000+ Java MCQs focuses on class fundamentals & object declaration in Java Programming Language.

1. 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
View Answer

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.
advertisement
advertisement

2. Which of these keywords is used to make a class?
a) class
b) struct
c) int
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

3. Which of the following is a valid declaration of an object of class Box?
a) Box obj = new Box();
b) Box obj = new Box;
c) obj = new Box();
d) new Box obj;
View Answer

Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. Which of these operators is used to allocate memory for an object?
a) malloc
b) alloc
c) new
d) give
View Answer

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.

5. Which of these statement is incorrect?
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
View Answer

Answer: a
Explanation: Every class does not need to have a main() method, there can be only one main() method which is made public.
advertisement

6. What will be the output of the following Java program?

  1.     class main_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int x = 9;
  6.             if (x == 9) 
  7.             { 
  8.                 int x = 8;
  9.                 System.out.println(x);
  10.             }
  11.         } 
  12.     }

a) 9
b) 8
c) Compilation error
d) Runtime error
View Answer

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

7. Which of the following statements is correct?
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
View Answer

Answer: a
Explanation: None.

8. What will be the output of the following Java program?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.     } 
  7.     class mainclass 
  8.     {
  9.         public static void main(String args[]) 
  10.         {        
  11.              box obj = new box();
  12.              obj.width = 10;
  13.              obj.height = 2;
  14.              obj.length = 10;
  15.              int y = obj.width * obj.height * obj.length; 
  16.              System.out.print(y);
  17.         } 
  18.     }

a) 12
b) 200
c) 400
d) 100
View Answer

Answer: b
Explanation: None.
output:

$ javac mainclass.java
$ java mainclass
200

9. What will be the output of the following Java program?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.     } 
  7.     class mainclass 
  8.     {
  9.         public static void main(String args[]) 
  10.         {        
  11.             box obj1 = new box();
  12.             box obj2 = new box();
  13.             obj1.height = 1;
  14.             obj1.length = 2;
  15.             obj1.width = 1;
  16.             obj2 = obj1;
  17.             System.out.println(obj2.height);
  18.         } 
  19.     }

a) 1
b) 2
c) Runtime error
d) Garbage value
View Answer

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

10. What will be the output of the following Java program?

  1.    class box 
  2.    {
  3.         int width;
  4.         int height;
  5.         int length;
  6.    } 
  7.     class mainclass 
  8.     {
  9.         public static void main(String args[]) 
  10.         {        
  11.             box obj = new box();
  12.             System.out.println(obj);
  13.         } 
  14.     }

a) 0
b) 1
c) Runtime error
d) classname@hashcode in hexadecimal form
View Answer

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

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.