Java Aptitude Question 1
Consider the java code:
public class interview
{
public static void main(String[] arr)
{
System.out.println(“first main”);
}
public static void main(String arr)
{
System.out.println(“second main”);
}
}
What is the output of the above code?
- second main
- first main
- first mainsecond main
- second mainfirst main
Answer
B. first main
Solution
In the above code, the main() method is overloaded.
However, JVM only recognizes the main method, which is defined with a String[] as the parameter or arguments.
Hence first main is printed, and the overloaded main method is ignored. That is second main is not printed.
Thus option B is correct.
Java Aptitude Question 2
Consider the following program given below
public class print{
public static void main(String[] args)
{
short x = 32767;
x++;
x++;
System.out.print(x);
}
}
What does the above code generate as an output?
- 32769
- -32767
- 2
- No output
Answer
B. -32767
Solution
In java, the size of the short is 2 bytes.
The short ranges as -215 to 215-1, that is, -32768 to 32767.
In java, all the datatypes are cyclic in nature. Therefore, two post-increment statements in the code increment in cyclic form as it goes in the negative form.
So the final output that is printed as is -32767.
Hence option B is correct.
Java Aptitude Question 3
What is the output of this program?
public class array{
public static void main(String[] args)
{
int[] a = {011,120, 002, 016,010};
for(int j = 0; j < a.length; j++)
{
System.out.print(a[j] + “ “);
}
}
}
- 011 120 002 016 010
- 9 120 2 14 8
- 11 120 2 16 10
- No output
Answer
B. 9 120 2 14 8
Solution
In the programming language, the octal number is represented by adding 0 as the prefix.
Therefore, in the program, some number is represented in octal form for the output it converts into decimal.
Octal | Decimal |
011 | 9 |
002 | 2 |
016 | 14 |
010 | 8 |
Thus the output is printed as 9 120 2 14 8.
Hence option B is correct.
Java Aptitude Question 4
Consider the program.
public class array{
public static void main(String[] args)
{
String a = new String(“BYJUSEXAMPREP”);
String b = new String(“BYJUSEXAMPREP”);
Strinc c = “BYJUSEXAMPREP”;
String d = “BYJUSEXAMPREP”;
}
}
How many instance or object is created of the above given program?
- 2
- 4
- 3
- 1
Answer
C. 3
Solution
Every time the new keyword is used, an object is created. As a result, two objects are produced for the first two statements in the program given above.
After that, the third statement creates another object as a string is declared.
The fourth statement does not generate an additional object because the string " BYJUSEXAMPREP " already exists.
Therefore, the correct answer is 3.
Thus option C is correct.
Java Aptitude Question 5
Consider the code given below
import java.util.*;
class Arraylists
{
public static void main(String args[])
{
ArrayLists arrlist = new ArrayLists();
arrlist.add("B");
arrlist.add("J");
arrlist.add("U");
arrlist.add("S");
arrlist.add(1, "Y");
System.out.println(arrlist);
}
}
What output is generated by the above code?
- BYJUS
- BJUSY
- YBJUS
- BYJUS1
Answer
A. BYJUS
Solution
arrlist is an ArrayLists object, which means it's a dynamic array that can grow and shrink in size.
The arrlist.add("X") adds element X to the array and arrlist.add(1,"X") adds element X at index position 1 in the list and shifts the previous value stored at that position by 1.
arrlist.add(1,"Y") stores Y at index position 1 of arrlist and shifts the previous value stored at that position by 1.
Hence the output is printed as BYJUS.
Therefore option A is correct.
☛ Related Questions:
Comments
write a comment