Study and learn Interview MCQ Questions and Answers on Java Classes and Objects. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes Classes and Java Theory Notes on Objects before reading these objective questions.
//Testing1.java public class Example { } public class Testing1 { public static void main(String[] args) { System.out.println("Hello Boss.!"); } }
There can not be more than one public class declared inside a single java file.
//bingo.java file public class Hello { public static void main(String[] args) { System.out.println("BINGO"); } }
The class name and the java file name should be the same. So, change either file name or class name to match.
class Fox { int legs = 2; } class Testing2 { public static void main(String[] args) { Fox t1 = new Fox(); System.out.println("T1 before: " + t1.legs); t1.legs = 4; System.out.println("T1 After: " + t1.legs); } }
T1 before: 4 T1 After: 4
T1 before: 2 T1 After: 2
T1 before: 2 T1 After: 4
There can be any number of classes in a single .java file.
class Food { int items; int show() {return items;} } class Testing9 { public static void main(String[] args) { Food f = new Food(); f.items = 5; System.out.println("Items Before = " + f.show()); change(f); System.out.println("Items After = " + f.show()); } static void change(Food foo) { foo.items = 10; } }
Items Before = 10 Items After = 10
Items Before = 5 Items After = 5
Items Before = 5 Items After = 10
Items Before = 10 Items After = 5
class Testing10 { int rats = 5; public static void main(String[] args) { Testing10 t1 = new Testing10(); System.out.println("Rats Before = " + t1.rats); modify(t1.rats); System.out.println("Rats After = " + t1.rats); } static void modify(int r) { r = 20; } }
Rats Before = 5 Rats After = 5
Rats Before = 20 Rats After = 20
Rats Before = 5 Rats After = 20
Rats Before = 20 Rats After = 5
The primitive values are passed by value only. So, changes in the method modify does not change the original value.
Yes. That is the reason why you can change the values of variables of the object using another reference.
References point to the original objects. So they can change the state of the objects.
Yes. The address is passed automatically by Java. So, Java pundits argue that it is passing a value (Address).
Yes. java.lang.Object is the superclass to all Java classes.
Yes. The methods are wait(), notify() and notifyAll().
Java collection classes use the hashcode() method to determine the equality of two objects.
class College { public String toString() { return "College Object"; } } class Testing18 { public static void main(String[] args) { College col = new College(); System.out.println("Printing Object=" + col); } }
print() and println() methods call toString() method of objects automatically.
class Cricket { int runs; } class Testing19 { public static void main(String[] args) { Cricket c1 = new Cricket(); c1.runs = 250; Cricket c2; c2 = c1; c2.runs = 300; System.out.println("Runs= " + c1.runs); } }
The reference C2 also points to the same object pointed by reference C1.
class Wordpress { int posts; } class Testing20 { public static void main(String[] args) { Wordpress wp1 = new Wordpress(); wp1.posts = 25; Wordpress wp2 = wp1; wp1 = null; System.out.println("Posts=" + wp2.posts); } }
Even if one REFERENCE to the same object is alive, it can be used to access the object. So, wp2 still works even if wp1 is set to null.