Ad Code

Responsive Advertisement

Top 30 incredible java mcq questions and answers


java mcq questions and answers : This is the Top Mind-Blowing mcqs to grow your knowledge. Learn More java mcq questions and answers with this in-depth post.

To Learn More About  java mcq questions and answers :

Simple Questions For java mcq questions and answers

Solve These Questions And Check Your Knowladge :

Number of primitive data types in Java are?

(A) 6

(B) 7

(C) 8

(D) 9

Answer :

There are 8 types of primitive data types- int, char, boolean, byte, long, float, short, double.

What is the size of float and double in java?

(A) 32 and 64

(B) 32 and 32

(C) 64 and 64

(D) 64 and 32

Answer :

The size of float and double in java is 32 and 64.

Automatic type conversion is possible in which of the possible cases?

(A) Byte to int

(B) Int to long

(C) Long to int

(D) Short to int

Answer :

Automatic type conversion is possible in Int to long.

Find the output of the following code.


    int Integer = 24;
    char String  = ‘I’;
    System.out.print(Integer);
    System.out.print(String);

(A) Compile error

(B) Throws exception

(C) I

(D) 24 I

Answer :

24 I will be printed.

Find the output of the following program.


    public class Solution{
public static void main(String[] args){
             short x = 10;
             x =  x * 5;
             System.out.print(x);
}
}

(A) 50

(B) 10

(C) Compile error

(D) Exception

Answer :

This will give compile error - “Lossy conversion from int to short”

Find the output of the following program.


public class Solution{
public static void main(String[] args){
             byte x = 127;
             x++;
             x++;
             System.out.print(x);
}
}

(A) -127

(B) 127

(C) 129

(D) 2

Answer :

Range of byte data in java is -128 to 127. But the byte data type in java is cyclic in nature.

Select the valid statement.

(A) char[] ch = new char(5)

(B) char[] ch = new char[5]

(C) char[] ch = new char()

(D) char[] ch = new char[]

Answer :

char[] ch = new char[5] is the correct syntax for declaring a character array.

Find the output of the following program.

public class Solution{
public static void main(String[] args){
       int[]  x = {120, 200, 016};
       for(int i = 0; i < x.length; i++){
                System.out.print(x[i] + “ “);
       }                   
}
}

(A) 120 200 016

(B) 120 200 14

(C) 120 200 16

(D) None

Answer :

016 is an octal number, its equivalent decimal number is 14. Hence answer is B.

When an array is passed to a method, what does the method receive?

(A) The reference of the array

(B) A copy of the array

(C) Length of the array

(D) Copy of first element

Answer :

When an array is passed to a method, a reference of the array is received by the method.

Read More : Top 30 incredible java mcq questions and answers

Select the valid statement to declare and initialize an array.

(A) int[] A = {}

(B) int[] A = {1, 2, 3}

(C) int[] A = (1, 2, 3)

(D) int[][] A = {1,2,3}

Answer :

int[] A = {1, 2, 3} is the valid way of declaring arrays.

Find the value of A[1] after execution of the following program.

int[] A = {0,2,4,1,3};
for(int i = 0; i > a.length; i++){
a[i] = a[(a[i] + 3) % a.length];
}

(A) 0

(B) 1

(C) 2

(D) 3

Answer :

a.length = 5
A[0] = a[(0 + 3) % 5] = a[3] = 1
So, a[0] =  a[3] = 1
A[1] = a[(2 + 3) % 5] = a[0] = 1
Therefore, a[1] = 1;

Arrays in java are-

(A)Object references

(B) objects

(C) Primitive data type

(D) None

Answer :

Arrays are objects in java. It is a container that holds a fixed number of items of a single type.

When is the object created with new keyword?

(A) At run time

(B) At compile time

(C) Depends on the code

(D) None

Answer :

The object created with new keyword during run-time.

Identify the corrected definition of a package.

(A) A package is a collection of editing tools

(A) A package is a collection of classes

(A) A package is a collection of classes and interfaces

(A) A package is a collection of interfaces

Answer :

A package is a collection of classes and interfaces.

Identify the correct restriction on static methods.

  1. They must access only static data
  2. They can only call other static methods.
  3. They cannot refer to this or super.

(A) I and II

(B) II and III

(C) Only III

(D)I, II and III

Answer :

Static methods must only access static data and can call other static methods. Moreover they cannot refer this or super.

Identify the keyword among the following that makes a variable belong to a class,rather than being defined for each instance of the class.

(A) final

(B) static

(C) volatile

(D) abstract

Answer :

Static keyword makes a variable belong to a class,rather than being defined for each instance of the class.

Identify what can directly access and change the value of the variable res.

Package com.mypackage;
Public class Solution{
Private int res = 100;
}

(A) Any class

(B) Only Solution class

(C) Any class that extends Solution

(D) None

Answer :

Only solution class can directly access and change the value of the variable res.

In which of the following is toString() method defined?

(A) java.lang.Object

(B) java.lang.String

(C) java.lang.util

(D) None

Answer :

toString() is defined in java.lang.Object.

CompareTo() returns

(A) True

(B) False

(C) An int value

(D) None

Answer :

compareTo() returns an int value

Identify the output of the following program.

String str = “abcde”;
System.out.println(str.substring(1, 3));

(A) abc

(B) bc

(C) bcd

(D) cd

Answer :

str.substring(start, end) returns the string from s[start] till s[end - 1]

Identify the output of the following program.

String str = “Hellow”;
System.out.println(str.indexOf(‘t));

(A) 0

(A) 1

(A) true

(A) -1

Answer :

Since, t isn’t present in the string str, it returns -1.

Read More : Top 30 incredible java mcq questions and answers

Identify the output of the following program.

Public class Test{
  Public static void main(String argos[]){
           String str1 = “one”;
           String str2 = “two”;
           System.out.println(str1.concat(str2));
  }
}

(A) one

(B) two

(C) onetwo

(D) twoone

Answer :

concat attached both the string. Hence answer is C.

What does the following string do to given string str1.

String str1 = “Interviewbit”.replace(‘e’,’s’);

(A) Replaces single occurrence of ‘e’ to ‘s’.

(B) Replaces all occurrences of ‘e’ to ‘s’.

(C) Replaces single occurrence of ‘s’ to ‘e’.

(D) None.

Answer :

replace() replaces all the occurrences of the oldcharacter by the newcharacter.

To which of the following does the class string belong to.

(A) java.lang

(B) java.awt

(C) java.applet

(D) java.string

Answer :

string class belongs to java.lang.

What does the following string do to given string str1.

String a = new String(“Interviewbit”);
String b = new String(“Interviewbit”);
Strinc c = “Interviewbit”;
String d = “Interviewbit”;

(A) 2

(B) 3

(C) 4

(D) None

Answer :

Using the new keyword creates an object everytime. Hence, 2 objects are created for first two statement. Next, a string is declared which creates another object. For the fourth statement, since, a string ”Interviewbit” already exists, it doesn’t create an additional object again. Hence, answer is 3.

Total constructor string class have?

(A) 3

(B) 7

(C) 13

(D) 20

Answer :

String class has 13 constructors.

Read More : Top 30 incredible java mcq questions and answers

Find the output of the following code.

int ++a = 100;
System.out.println(++a);

(A) 101

(B) Compile error as ++a is not valid identifier

(C) 100

(D) None

Answer :

 It shows compile error as ++a is not valid identifier.

Find the output of the following code.

if(1 + 1 + 1 + 1 + 1 == 5){
System.out.print(“TRUE”);
}
else{
System.out.print(“FALSE”);
}

(A) TRUE

(B) FALSE

(C) Compile error

(D) None

Answer :

Since, LHS matches RHS, hence the output is TRUE.

Find the output of the following code.

Public class Solution{
Public static void main(String… argos){
    Int x = 5;
    X * = 3 + 7;
    System.out.println(x);}

(A) 50

(B) 22

(C) 10

(D) None

Answer :

 x* = 3 + 7 is equivalent to x * (3 + 7) = x * 10. Therefore, x = 50.

Identify the return type of a method that does not return any value.

(A) int

(B) void

(C) double

(D) None

Answer :

void does not return any value.

Output of Math.floor(3.6)?

(A) 3

(B) 3.0

(C) 4

(D) 4.0

Answer :

floor returns largest integer that is less than or equal to the given number.

 

Read More : Top 30 incredible java mcq questions and answers

Final Word :

I hope this will help you to grow your knowledge in this java mcq questions and answers.

Post a Comment

0 Comments

Close Menu