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

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.

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.

6. What is the output of this program?

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

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:
$ 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 is the output of this program?

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

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

Answer: b
Explanation: None.
output:
$ javac mainclass.java
$ java mainclass
200

9. What is the output of this program?

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

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 is the output of this program?

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

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

Answer: d
Explanation: Object obj of box class contains reference to the memory which was given to its class instances. Printing obj will print the address of the memory.
output:
$ javac mainclass.java
$ java mainclass
box@130671e

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry