hamburger

Java Aptitude Questions and Answers

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Candidates who are actively preparing for the placement can get Java aptitude questions and answers here. Java aptitude questions and answers might provide a brief idea of the types of questions you will face in the GATE and other related examinations.

Java aptitude will be better understood by using objective-based MCQ questions and answers. 5 important questions are mentioned here based on the recent GATE CSE syllabus. Attempt and get the detailed answer to each Java aptitude question mentioned in this article.

Download Compiler Design Formula Notes PDF

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?

  1. second main
  2. first main
  3. first mainsecond main
  4. 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?

  1. 32769
  2. -32767
  3. 2
  4. 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] + “ “);

}

}

}

  1. 011 120 002 016 010
  2. 9 120 2 14 8
  3. 11 120 2 16 10
  4. 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?

  1. 2
  2. 4
  3. 3
  4. 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?

  1. BYJUS
  2. BJUSY
  3. YBJUS
  4. 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:

A pointer is?

The section with the maximum shape factor is the___.

Which is not a valid constructor of thread class

Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium