classes in java and Objects Interview MCQ Questions and Answers
Table of Contents
Study and learn Interview MCQ Questions and Answers on classes in java and Objects. Attend job interviews easily with these Multiple Choice Questions.
Java Classes Structure or Syntax Overview Tutorial
A Java program consists of a CLASS with some Code and Data. Code is called Method(s). Data is called Variable(s). Java is an Object-Oriented Language. Let us know more about Java Class Structure and Syntax in this Last Minute Tutorial.
Java Classes Structure and Syntax Overview
A Java Class is like a wrapper or envelope that comprises a number of Methods and Variables. A Java class is like a Blue-Print or Specification-Sheet or Prototype that defines how an Object instantiated out of it (class) to behave and store data.
types of classes in java
A Java Class broadly consists of the following.
- Variables
- Methods
- Constructors
Java Classes Syntax
class CLASSNAME { type1 var_1 type2 var_2; type method1(param1,param2) { //code } }
Rules For Creating a Class:
- Each class should contain an Opening Brace “{” and a Closing Brace “}”.
- and Each method has its own Opening and Closing Braces.
- Each Java class uses the keyword “class” followed by a valid CLASSNAME.
- CLASSNAME or simply “Name” can be any valid identifier.
- and CLASSNAME can start with only a Character Alphabet, Underscore (_) or a Dollar ($) sign.
- CLASSNAME can not start with a number or digit. But, numbers may come in the middle or end of the NAME.
- In a .java file, there can only be one “public class“. Other classes defined without specifying “public” access specifier.
- There is no restriction on the number of methods present in a class.
- A Java method may or may not return a typed-value.
- and A Java method can be public, private, protected or default.
- A Java variable can be public, private, protected or default.
- The MAIN method is not necessary.
- Some projects or frameworks do not allow adding a “main” method to the Java program.
- Some complex Java classes contain “implements” or “extends” keywords next to the CLASSNAME followed by predefined Class-Names. This is part of Inheritance.
Creating a Java Class Object and Using it
A Java object is like a variable of a particular data-type. Here the Object is of the CLASS-type we define. A Class is of no use unless we create Objects. All objects are created at run-time dynamically. Creating an object involves allocating memory to it and returning a reference or handle of the memory location. We create Java objects using a “new” keyword.
A Java Class exists only theoretically or virtually. But an object exists really in memory. A class is comparable to a Structure in a C programming language.
In the above Infographic, we tried to explain what really an Object is. Here, Cupcake Pan is like a prototype. Assume, it has only one mould. It defines what should be the shape of a final Object. Using methods like Kneading, we turned Variables like Ingredients into useful Cupcakes or Objects. Remember that each Object is unique and has its own properties.
An example of Java Classes
.A House object “obj” is created using the “new” keyword. The keyword “new” tells the Java Runtime to allocate some memory to the House object and return a reference to “obj“. Now, the object can be accessed using the reference obj.
Each House object has two properties or variables namely width and height. Method getArea() calculates are of the House object and returns it.
public class House { int width, height; int getArea() { return width*height; } public static void main(String[] args) { House obj = new House(); obj.width = 10; obj.height= 20; int area = obj.getArea(); System.out.println("Area of the house = " + area); } } //OUTPUT //Area of the house = 200
[WpProQuiz 156]
Classes in java MCQ
1) Java is a ___ programming language.
A) Functional
B) Object-Oriented
C) Theoretical
D) All the above
Answer [=] B
Explanation:
Everything in Java is implemented using Object-Oriented principles.
2) In Java programming language, the code is placed inside ___.
A) Classes, Interfaces
B) Methods
C) Blocks
D) All the above
Answer [=] D
3) Properties are implemented using ___ in Java.
A) Methods
B) Variables
C) Interfaces
D) All the above
Answer [=] B
4) A Class in Java is like a ____.
A) Prototype
B) Instruction Sheet
C) Blueprint
D) All the above
Answer [=] D
5) Which is the file extension used for a public Java class source code?
A) .j
B) .class
C) .java
D) None
Answer [=] C
6) Which is the file extension used for a compiled Java class file?
A) .j
B) .java
C) .class
D) .cls
Answer [=] C
7) State TRUE or FALSE. The source-code of An Abstract-Class or Interface is kept inside a .java file.
A) FALSE
B) TRUE
Answer [=] B
8) After compilation, an Interface or Abstract-Class is kept in a ___ file in Java programming.
A) .java
B) .cls
C) .class
D) .interface
Answer [=] C
Explanation:
Only a.class file contains all of the Java classes, interfaces,. And abstract classes that have been compiled. The.java files contain all of the source files.
9) State TRUE or FALSE. In Java, a public class or abstract-class or interface must be kept in a separate .java file.
A) FALSE
B) TRUE
Answer [=] B
10) In a .java file, how many numbers of public types namely class, interface or abstract can be managed?
A) 1
B) 2
C) 3
D) Any number
Answer [=] A
Explanation:
Only one public type is allowed per .java file.
11) In Java, the keyword used to declare a class is ___.
A) Class
B) Java
C) class
D) java
Answer [=] C
12) A Java class can contain___.
A) Variables
B) Methods, Constructors
C) Inner Classes (A class inside another class)
D) All the above
Answer [=] D
13) How many maximum numbers of objects can be created from a single Class in Java?
A) 32
B) 64
C) 256
D) None
Answer [=] D
Explanation:
The amount of objects that can produced from a class is unlimited.
14) Creating an object from a class is also called ____.
A) Initializing
B) Instantiating
C) Interfacing
D) None of the above
Answer [=] B
15) The keyword used to create a new object in Java is ___.
A) class
B) java
C) new
D) create
Answer [=] C
16) Choose the correct statements about choosing a name for a class in Java.
A) The class name can start with only a letter or underscore or dollar sign.
B) The class name can contain numbers
C) The class name can not start with a number
D) All the above
Answer [=] D
Explanation:
Java naming rules are another name for these.
17) An object is created at __ time in Java.
A) Compile-time
B) Run time
C) Assembling time
D) None of the above
Answer [=] B
18) Choose the correct statement about Java main method.
A) The main method is not a required method
B) The main method must declared public static void.
C) you can define program flow using the main method. The Java virtual machine calls the main method directly.
D) All the above
Answer [=] D
Explanation:
Java compiler does not complain about a missing main method.
19) Choose the correct syntax for declaring a Java class below.
A)
class CLASSNAME { }
B)
CLASSNAME class { }
C)
class CLASSNAME; { }
D)
Class CLASSNAME { }
Answer [=] A
Explanation:
Use the keyword class but not Class.
20) Choose the correct way of creating an object of the below class.
class Table { Table(){System.out.println("Table Created");} }
A)
Table t = new Table;
B)
Table t = new Table();
C)
Table() t = new Table();
D) None of the above
Answer [=] B