This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Data Members”.
1. What will be the output of the following Java code?
class Books { abstract int price; Books(int p) { this.price = p; } public void getPrice() { System.out.println("Price: "+this.price); } } public class AbstractDataMembers { public static void main(String[] args) { Books b = new Books(20); b.getPrice(); } }
a) Price: 20
b) 20
c) Compilation error
d) Runtime error
View Answer
Explanation: An abstract modifier in java is a non-access modifier which can only be applied to classes and methods but not for variables. In the program, we marked the instance data member “price” as an abstract, so we get a compilation error.
Output:
$ javac AbstractDataMembers.java error: modifier abstract not allowed here.
2. What will be the output of the following Java code?
import java.io.*; class TransientVariables implements Serializable { int num1 = 20; transient int num2 = 30; public static void main(String[] args) throws Exception { TransientVariables t = new TransientVariables(); FileOutputStream fo = new FileOutputStream("input.txt"); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(t); FileInputStream fi = new FileInputStream("input.txt"); ObjectInputStream oi = new ObjectInputStream(fi); TransientVariables t1 = (TransientVariables) oi.readObject(); System.out.println("num1: " + t1.num1); System.out.println("num2: " + t1.num2); } }
a)
num1: 20 num2: 30
b)
num1: 0 num2: 30
c)
num1: 20 num2: 0
d)
num1: 0 num2: 0View Answer
Explanation: Instance data members can be transient in java. In the program, we declared the instance data member “num2” as transient. The transient keyword makes the JVM ignore the original value of the variable “num2” (20) at the time of serialization and saves the default value of the variable’s data type. As “num2” is an integer, it saves 0 as default value.
Output:
$ javac TransientVariables.java $ java TransientVariables 20 0
3. What will be the output of the following Java code?
//Create a directory/folder named p1, and save a Java file(Vehicle.java) package p1; public class Vehicle { protected int color; protected int speed = 10; } //Create another directory named p2, and save a Java file(ProtectedVariables.java) and make sure that two packages should be under the same root directory. //another program package p2; import p1.Vehicle; //import package p1 public class ProtectedVariables extends Vehicle { public static void main(String[] args) { ProtectedVariables p = new ProtectedVariables(); System.out.println("Speed: "+p.speed); } }
a) Speed: 10
b) Compilation error
c) Runtime error
d) Speed: 0
View Answer
Explanation: We can access the Protected modifiers outside the package by using Inheritance. In the program, we have accessed the protected variable “speed” outside the package (p1) by importing the package(p1) in p2.
Output:
$ javac -cp . p1\Vehicle.java p2\ProtectedVariables.java $ java -cp . p2.ProtectedVariables Speed: 10
4. What will be the output of the following Java code?
class Book { String title; String author; Book(String t) { this.title = t; } } public class InstanceMembers { public static void main(String[] args) { Book b = new Book("Java Programming"); System.out.println("Book Title: "+Book.title); } }
a) Book Title: Java Programming
b) Compilation error
c) Runtime error
d) Java Programming
View Answer
Explanation: An instance variable is accessed using the respective object name. In the program, we are accessing it by calling the class name, so we get an error. Instance data members are also known as “Object Level Data Members” because they depend on object names instead of class names.
Output:
$ javac InstanceMembers.java error: non-static variable title cannot be referenced from a static context.
5. What will be the output of the following Java code?
class Employee { int salary = 1000000; static float experience = 4.5f; } public class StaticDatamembers { public static void main(String[] args) { Employee e = new Employee(); System.out.println(e.experience); System.out.println(Employee.experience); } }
a)
4.5 4.5
b)
4.5 Compilation error
c)
Compilation error 4.5
d) Runtime error
View Answer
Explanation: Unlike the Instance variable, we can access static variables through objects or directly using class names. As the variable “experience” in the program is static, we don’t get an error.
Output:
$ javac StaticDatamembers.java $ java StaticDatamembers 4.5 4.5
6. What will be the output of the following Java code?
class Student { protected String name = "Java"; } class ProtectedVariables { public static void main(String[] args) { Student s = new Student(); System.out.println(s.name); } }
a) Compilation error
b) Runtime error
c) Instance variables cannot be protected
d) Java
View Answer
Explanation: The protected access modifier is accessible within the package. However, it can also be accessible outside the package but through the inheritance only.
Output:
$ javac ProtectedVariables.java
$ java ProtectedVariables
Java
7. What will be the output of the following Java code?
class Area { int side; public int squareArea() { int a = this.side * this.side; return a; } } public class InstanceVariables { public static void main(String[] args) { Area a = new Area(); System.out.println("Area: "+a.squareArea()); } }
a) Initialize instance variables before use
b) Compilation error
c) Area: 0
d) Runtime error
View Answer
Explanation: Instance variables in java are initialized to their default values if the code doesn’t explicitly initialize them. In the program, we didn’t initialize the variable “side”, so, by default, we get the value as 0 because the variable “side” is an integer.
Output:
$ javac InstanceVariables.java $ java InstanceVariables Area: 0
8. What will be the output of the following Java code?
class Distance { int time; static int speed = 10; static { int d = speed * time; } } public class StaticVariables { public static void main(String[] args) { Distance d = new Distance(); System.out.println("Speed: "+d.speed); System.out.println("Time: "+d.time); } }
a) Speed: 10
b) Compilation error
c) Runtime error
d)
Speed: 10 Time: 0View Answer
Explanation: Static methods/blocks can only use those instance variables that are static. But in the program, the instance variable “time” is not static, so we get a compilation error as we are using the non-static variable in the static block.
Output:
$ javac StaticVariables.java error: non-static variable time cannot be referenced from a static context
9. What will be the output of the following Java code?
class Interest { int principal = 1000; int time = 2; static int rate = 3; static { Interest i = new Interest(); int interest = (i.principal * i.time * rate)/100; System.out.println("Simple Interest: "+interest); } } public class StaticMembers { public static void main(String[] args) { Interest i = new Interest(); } }
a) Simple Interest: 600
b) Compilation error
c) Runtime error
d) Simple Interest: 60
View Answer
Explanation: We can use the instance variables inside the static blocks by creating the instance of a class. In the program, we used the non-static variables (principal, time) by creating the instance of the class.
Output:
$ javac StaticMembers.java $ java StaticMembers Simple Interest: 60
10. What will be the output of the following Java code?
import java.io.*; class TransientAndFinal implements Serializable { int num1 = 50; transient final int num2 = 100; public static void main(String[] args) throws Exception { TransientAndFinal t = new TransientAndFinal(); FileOutputStream fo = new FileOutputStream("input.txt"); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(t); FileInputStream fi = new FileInputStream("input.txt"); ObjectInputStream oi = new ObjectInputStream(fi); TransientAndFinal t1 = (TransientAndFinal) oi.readObject(); System.out.println("num1: " + t1.num1); System.out.println("num2: " + t1.num2); } }
a)
num1: 50 num2: 100
b)
num1: 0 num2: 100
c)
num1: 50 num2: 0
d)
num1: 0 num2: 0View Answer
Explanation: The instance variable “num2” is both final and transient. Final variables are directly serialized by their values, so there is no impact of declaring the final variable “num2” as transient.
Output:
$ javac TransientAndFinal.java $ java TransientAndFinal 50 100
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.