This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Assigning Objects”.
1. What will be the output of the following Java code?
public class Main { int a, b; Main(int i, int j) { a = i; b = j; } public static void main(String[] args) { Main a = new Main(27, 2); Main b = a; System.out.println(a.a); System.out.println(b.b); } }
a)
27 2
b) Runtime Error
c)
2 27
d) Compilation Error
View Answer
Explanation: The object a is assigned to b, hence linking both to the same reference. Therefore they access the same elements when called. Since they both refer to the same reference, the values will be printed in the same order as they are assigned i.e. 27 and 2.
Output:
$ javac Main.java $ java Main 27 2
2. What will be the output of the following Java code?
public class Main { String s; Main(String a) { s = a; } public static void main(String[] args) { Main a = new Main("Assigning Object"); Main b = a; System.out.println(b.s.charAt(4)); } }
a) n
b) Compilation Error
c) g
d) Runtime Error
View Answer
Explanation: The object a is assigned to b, linking them to the same reference, hence it can call the string s of the object a. The charAt function returns the character at the specified index in the string. As indexing starts at 0, the nth index refers to the (n+1)th position.
Output:
$ javac Main.java
$ java Main
g
3. What will be the output of the following Java code?
import java.util.*; public class Main { int a, b; Main(int a, int b) { this.a = a; this.b = b; } void area() { System.out.println(a*b); } public static void main(String[] args) { int height = 10; int breadth = 20; Main a = new Main(height, breadth); Main b = a; a.a = 30; b.b = 40; b.area(); } }
a) 200
b) 1200
c) 2400
d) 800
View Answer
Explanation: As the objects a and b refer to the same reference, changing the element of one object changes it for the other object as well, hence the values are changed for both objects.
Output:
$ javac Main.java $ java Main 1200
4. What will be the output of the following Java code?
import java.util.*; public class Main { int a, b; Main(int a, int b) { this.a = a; this.b = b; } Main(Main s) { a = s.a; b = s.b; } public static void main(String[] args) { Main x = new Main(20, 40); Main y = x; Main z = new Main(x); z.a = 15; System.out.println(x.a); } }
a) 15
b) 20
c) 40
d) Compilation Error
View Answer
Explanation: Z is an object copied from x using the copy constructor, hence they do not correspond to the same reference. Therefore changing an element in z doesn’t affect x or y’s elements.
Output:
$ javac Main.java $ java Main 20
5. What will be the output of the following Java code?
public class Main { int a, b; Main(int a, int b) { this.a = a; this.b = b; } void Print() { System.out.println(this); } public static void main(String[] args) { Main x = new Main(20, 40); Main y = x; y.Print(); } }
a) 20 40
b)
20 40
c) Main@2a139a55
d) Compilation Error
View Answer
Explanation: When an object is printed, it is printed in the format ClassName@HashCode.
Output:
$ javac Main.java
$ java Main
Main@2a139a55
6. What will be the output of the following Java code?
public class Main { int a, b; Main(int a, int b) { this.a = a; this.b = b; } public static void main(String[] args) { Main x = new Main(40, 80); Main y = x; System.out.println(x); System.out.println(y); } }
a)
Main@2a139a55 Main@4a210c21
b)
Main@2a139a55 Main@2a139a55
c)
20 40 20 40
d) Compilation Error
View Answer
Explanation: When an object is printed, it is printed in the format ClassName@HashCode. The objects x and y refer to the same objects as y is assigned to x. Hence they print the same output.
Output:
$ javac Main.java
$ java Main
Main@2a139a55
Main@2a139a55
7. What will be the output of the following Java code?
import java.util.*; public class Main { String s; Main(String a) { s = a; } void Position(char a) { System.out.println(s.indexOf(a)); } public static void main(String[] args) { Main a = new Main("Assigning Object"); Main b = a; b.Position('i'); } }
a) 3
b) 6
c) 4
d) 7
View Answer
Explanation: The indexOf function returns the first index of the specified character in the string calling it. Since indexing starts from 0, the nth position in a string refers to the (n-1)th index.
Output:
$ javac Main.java $ java Main 3
8. What will be the output of the following Java code?
public class Main { char ch; Main(char a) { ch = a; } void Ascii() { System.out.println((int)ch); } public static void main(String[] args) { Main a = new Main('J'); Main b = a; b.Ascii(); } }
a) 106
b) Compilation Error
c) Runtime Error
d) 74
View Answer
Explanation: When a character is type converted to integer, it converts to it’s ascii value. The ascii value of ‘j’ is 106, and that of ‘J’ is 74.
Output:
$ javac Main.java $ java Main 74
9. What will be the output of the following Java code?
import java.util.*; public class Main { int a; Main(int i) { a = i; } public static void main(String[] args) { Main a = new Main(20); Main b = a; System.out.println(b.getClass()); System.out.println(a.getClass().getSuperclass()); } }
a)
class Main class Object
b)
class Main class java.io.Object
c)
class Main class java.lang.Object
d) Compilation Error
View Answer
Explanation: A class is an object of the superclass Object of the lang package of Java. Therefore when the superclass of Main is called, it returns java.lang.Object.
Output:
$ javac Main.java $ java Main class Main class java.lang.Object
10. What will be the output of the following Java code?
import java.util.*; public class Main { LinkedList l = new LinkedList(); Main(int i) { l.add(i); } void push(int a) { l.add(a); } void print() { System.out.println(l); } public static void main(String[] args) { Main a = new Main(5); Main b = a; a.push(40); b.push(30); b.push(40); b.print(); a.print(); } }
a)
[5, 40, 30, 40] [5, 40, 30, 40]
b)
[5, 40] [30, 40]
c)
[5, 40] [5, 30, 40]
d) Compilation Error
View Answer
Explanation: As both objects a and b correspond to the same reference, any change in one object will reflect on the other.
Output:
$ javac Main.java $ java Main [5, 40, 30, 40] [5, 40, 30, 40]
11. What will be the output of the following Java code?
public class Main { int a, b; Main(int i, int j) { a = i; b = j; } public static void main(String[] args) { Main a = new Main(10, 2); Main b = a; b.a = 30; System.out.println(a.a<<b.b); } }
a) 120
b) 40
c) 10
d) 20
View Answer
Explanation: The left shift operator adds 0s to the binary form of the number at the end, thus increasing the value in multiples of 2. Here the bits are shifted by 2, hence it is equivalent by multiplying the number by 22, which is 4.
Output:
$ javac Main.java $ java Main 120
12. What will be the output of the following Java code?
import java.util.*; public class Main { LinkedList l = new LinkedList(); Main(int i) { l.add(i); } public static void main(String[] args) { Main a = new Main(20); Main b = a; b.l.add(40); System.out.println(a.l); System.out.println(b.l); } }
a)
[20, 40] [20, 40]
b)
[20] [40]
c)
[20] [20, 40]
d) Compilation Error
View Answer
Explanation: Objects a and b refer to the same reference as a is assigned to b with the assignment ( = ) operator, therefore a change in one object results in a change in both objects.
Output:
$ javac Main.java $ java Main [20, 40] [20, 40]
13. What will be the output of the following Java code?
import java.util.*; public class Main { LinkedList l = new LinkedList(); Main(int i) { l.add(i); } void add(int a) { l.add(a); } void sum() { int sum = 0; Iterator i = l.iterator(); while(i.hasNext()) sum+ = (int)i.next(); System.out.println(sum); } public static void main(String[] args) { Main a = new Main(35); Main b = a; a.add(45); b.add(32); b.add(64); a.add(23); a.sum(); b.sum(); } }
a)
103 61
b)
199 199
c)
199 103
d) Compilation Error
View Answer
Explanation: The object a is assigned to b, therefore a change in one results in a change in both as they both are linked to the same reference.
Output:
$ javac Main.java $ java Main 199 199
14. What will be the output of the following Java code?
public class Main { String s; Main(String a) { s = a; } public static void main(String[] args) { Main a = new Main("Sanfoundry"); Main b = a; System.out.println(a.s.indexOf('n')); System.out.println(b.s.indexOf('n', 3)); } }
a)
3 7
b)
3 3
c)
2 2
d)
2 6View Answer
Explanation: Both objects a and b refer to the same object as a is assigned to b. The indexOf function returns the index of the character in the string. If an integer is passed along with the character, it searches from that index.
Output:
$ javac Main.java $ java Main 2 6
15. What will be the output of the following Java code?
public class Main { String s; Main(String a) { s = a; } void change(String a) { s = a; } public static void main(String[] args) { Main a = new Main("San"); Main b = a; b.change("foundry"); System.out.println(b.s+a.s); } }
a) Compilation Error
b) Sanfoundry
c) foundryfoundry
d) foundrySan
View Answer
Explanation: The objects a and b are linked to the same reference because a is assigned to b, therefore changing something in one object changes it in both objects.
Output:
$ javac Main.java
$ java Main
foundryfoundry
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.