This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Memory Allocation”.
1. What will be the output of the following Java code?
public class Sanfoundry { public static void main(String[] args) { String s = new String("Sanfoundry"); System.out.println(s.charAt(2)); } }
a) Compilation Error
b) a
c) f
d) n
View Answer
Explanation: The new keyword is used to allot memory to the string object s. The function charAt returns the character at the specified index, and as indexing starts from 0, the index 2 corresponds to the third position.
Output:
$ javac Sanfoundry.java
$ java Sanfoundry
n
2. What will be the output of the following Java code?
public class Java { void print(String s) { System.out.println(s.indexOf('n')); } public static void main(String[] args) { Main a = new Main(); String s = new String("Sanfoundry"); a.print(s); } }
a) 3
b) 2
c) 7
d) 6
View Answer
Explanation: The new keyword is used to allot memory to the string object s and the object a. The indexOf function returns the index of the character specified in the string.
Output:
$ javac Java.java $ java Java 2
3. What will be the output of the following Java code?
public class Main { String s = new String("Memory Allocation"); void position() { System.out.println(s.indexOf('l')); } void replace() { s=s.replace("l", "L"); } public static void main(String[] args) { Main a = new Main(); a.replace(); a.position(); } }
a) 8
b) 9
c) -1
d) Compilation Error
View Answer
Explanation: The new keyword is used to allocate memory to the objects. The replace function replaces all the occurences of the string passed to the function with the other string passed to the function, hence changing all ‘l’s to ‘L’s.
Output:
$ javac Main.java $ java Main -1
4. What will be the output of the following Java code?
public class Memory { String s = new String("Sanfoundry"); void position() { System.out.println(s.indexOf('n')); } void replace() { s=s.replaceFirst("n", "N"); } public static void main(String[] args) { Memory a = new Memory(); a.replace(); a.position(); } }
a) 3
b) 7
c) 6
d) -1
View Answer
Explanation: The new keyword is used to allocate memory to the objects created. The replaceFirst function replaces the first instance of the first substring passed to it with the second substring passed to the function.
Output:
$ javac Memory.java $ java Memory 6
5. What will be the output of the following Java code?
public class Malloc { Integer h = new Integer(0); Integer b = new Integer(0); void Area() { System.out.println(h*b); } public static void main(String[] args) { Malloc a = new Malloc(); a.h=10; a.b=20; a.Area(); } }
a) 0
b) Compilation Error
c) 200
d) Runtime Error
View Answer
Explanation: The new keyword is used to allocate memory to the objects created. The Integer objects h and b are given initial values 0, which are then changed in the main function.
Output:
$ javac Malloc.java $ java Malloc 200
6. What will be the output of the following Java code?
public class Java { public static void main(String[] args) { Integer i = new Integer(10); Integer j = new Integer(3); System.out.println(i<<j); } }
a) 20
b) 30
c) 80
d) 120
View Answer
Explanation: The new keyword is used to allocate memory to the objects created. The left shift operator shifts the bits of the number to the left followed by 0s at the end. It is equivalent to multiplying the number by 2n where n is the number to the right of the shift operator.
Output:
$ javac Java.java $ java Java 80
7. What will be the output of the following Java code?
public class Memory { public static void main(String[] args) { Integer i = new Integer(10); String s = new String("Sanfoundry"); System.out.println(i.getClass().getSuperclass()); System.out.println(s.getClass().getSuperclass()); } }
a)
class java.lang.Number class java.lang.Object
b)
class java.io.Number class java.io.Object
c)
class java.util.Number class java.util.Object
d) Compilation Error
View Answer
Explanation: The new keyword is used to allocate memory to the integer and string objects. Integer is a subclass of the Number class of the lang package, and String is a subclass of the Object class of the lang package.
Output:
$ javac Memory.java $ java Memory class java.lang.Number class java.lang.Object
8. What will be the output of the following Java code?
public class Ascii { public static void main(String[] args) { Character ch = new Character('c'); System.out.println((int)ch); } }
a) 99
b) Compilation Error
c) 67
d) Runtime Error
View Answer
Explanation: The new keyword is used to assign memory to the character object ch. When a character is converted to an integer, it is converted based on it’s ascii value. The ascii value of ‘c’ is 99 and that of ‘C’ 67.
Output:
$ javac Ascii.java $ java Ascii 99
9. What will be the output of the following Java code?
import java.util.*; public class list { public static void main(String args[]) { LinkedList l = new LinkedList(); l.add(20); l.add(30); l.add(50); l.add(70); l.add(90); Collections.shuffle(l); System.out.println(l); } }
a) [90, 20, 70, 30, 50]
b) [90, 30, 20, 50, 70]
c) [70, 30, 20, 50, 90]
d) Random order
View Answer
Explanation: The new keyword is used to allot memory to the linked list l. The shuffle function of the Collections class is used to shuffle the order of the elements of the linked list passed to it as a parameter.
Output:
$ javac list.java $ java list [90, 20, 70, 30, 50] $ javac list.java $ java list [70, 30, 20, 50, 90]
10. What will be the output of the following Java code?
import java.util.*; public class lists { public static void main(String args[]) { LinkedList l = new LinkedList(); l.add(28); l.add(41); l.add(4); l.add(3); l.add(10); l.remove(4); System.out.println(l); } }
a) [28, 41, 4, 3]
b) [28, 41, 3, 10]
c) [28, 41, 4, 10]
d) Compilation Error
View Answer
Explanation: The new keyword is used to allot memory to the linked list l. THe remove function of the linked list is used to remove the element at the index specified in the value passed to the function, and as indexing starts at 0, 4th index corresponds to the 5th position.
Output:
$ javac list.java $ java list [28, 41, 4, 3]
11. What will be the output of the following Java code?
public class Char { public static void main(String args[]) { Integer i = new Integer(69); System.out.println((char)i); } }
a) E
b) e
c) Runtime Error
d) Compilation Error
View Answer
Explanation: The new keyword allots memory to the integer object i. As i is an integer object, it cannot be converted to a character.
Output:
$ javac Char.java $ java Char error: incompatible types: Integer cannot be converted to char
12. What will be the output of the following Java code?
public class Sanfoundry { public static void main(String args[]) { String s = "Sanfoundry"; String t = new String("Sanfoundry"); System.out.println(s==t); } }
a) true
b) Compilation Error
c) false
d) Runtime Error
View Answer
Explanation: The ‘==’ operator checks if the reference is the same when comparing two objects. As the new keyword is used to make object t, it is allotted a different memory location, hence they aren’t equal.
Output:
$ javac Sanfoundry.java $ java Sanfoundry false
13. What will be the output of the following Java code?
public class Empty { public static void main(String args[]) { String s = new String(" "); System.out.println(s.isEmpty()); System.out.println(s.trim().isEmpty()); } }
a)
true true
b)
true false
c)
false false
d)
false trueView Answer
Explanation: The new keyword allots memory to the string object s with an initial value of ” “. The trim function removes spaces at the start and end of the string. As this string has only spaces, it removes all of them. The isEmpty function checks if the string is empty or not.
Output:
$ javac Empty.java $ java Empty false true
14. What will be the output of the following Java code?
public class Objects { public static void main(String args[]) { Objects a = new Objects(); Objects b = a; Objects c = new Objects(); System.out.println(a); System.out.println(b); System.out.println(c); } }
a)
Objects@2a139a55 Objects@2a139a55 Objects@15db9742
b)
Objects@15db9742 Objects@15db9742 Objects@15db9742
c)
Objects@2a139a55 Objects@15db9742 Objects@15db9742
d) Compilation Error
View Answer
Explanation: The objects a and b refer to the same reference as a is assigned to b. The objects a and c refer to two different memory locations as they both are created using the new keyword which assigns them separate memory locations.
Output:
$ javac Objects.java
$ java Objects
Objects@2a139a55
Objects@2a139a55
Objects@15db9742
15. What will be the output of the following Java code?
public class MemAlloc { public static void main(String args[]) { int a[]= new int[]{1, 2, 3, 4, 5}; for(int i=1;i<=a.length;i++) { if(i%2==0) { System.out.println(a[i-1]); } } } }
a)
2 4
b)
1 3 5
c) Compilation Error
d)
1 2 3 4 5View Answer
Explanation: The new keyword allots memory to the integer array a. The for loop checks for even values of i and prints the elements at that position. As indexing starts at 0, the ith position is the (i-1)th index of that array.
Output:
$ javac MemAlloc.java $ java MemAlloc 2 4
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.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]