Java Test Questions

Java Questions


READ THE QUESTIONS CAREFULLY AND SELECT THE CORRECT OPTION.

1. Which of these is a type of variable in Java?

A)Instance Variable

B)Local Variable

C)Static Variable

D)All of these


2.What will be the output of following Java code?


public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        str = "Bye";
        System.out.println(str);

    }
}

A)Hello

B)Bye

C)Error

D)All of these


3. What will be the output of following Java code?


public class Main {
    public static void main(String arg[]) {
        int i;
        for (i = 1; i <= 12; i += 2) {
            if (i == 8) {
                System.out.println(i);
                break;

            }
        }
    }
}

A)1

B)No output

C)8

D)1357911


4) What part is used to compile, debug and execute java programs?

a) JRE

b) JIT

c) JDK

d) JVM


5) Which of these selection statements in Java do you have?

a) a break

b) continue

c) for ()

d) if ()


6) Arrays in java are-

a) Object References

b) Objects

c) Primitive Data type

d) None


7) Where does the system stores parameters and local variables whenever a method is invoked?

a) Heap

b) Stack

c) Objects

d) Reference


8) What will be the output of the following Java code?


class increment {
    public static void main(String args[]) {
        int g = 3;
        System.out.print(++g * 8);

    }
}

a) 25

b) 24

c) 32

d) 33


9) Which of these is an incorrect string literal?

a) “Hello World”

b) “Hello\nWorld”

c) “\”Hello World\””

d) "Hello world"


10) Which is the super class of all exception classes

A. Exception

B. Object

C. Error

D. Throwable


11) Thread priority in Java is represented as?

A. int

B. Float

C. double

D. long


12) Which of these methods can be used to write console output?

A. writeln()

B. println()

C. write()

D. All of the mentioned


13) Which refers to a channel through which data flow from the source to the destination:

A. String

B. Character

C. Stream

D. Buffer


14) What is it called where child object gets killed if parent object is killed?

a) Aggregation

b) Composition

c) Encapsulation

d) Association


15) What will be the output of the following Java program?


interface calculate {
    void cal(int item);
}

class display implements calculate {
    int x;
    public void cal(int item){
        x = item * item;
    }
}

class interfaces {
    public static void main(String args[]) {
        display arr = new display;
        arr.x = 0;
        arr.cal(2);
        System.out.print(arr.x);

    }
}

 a) 0

b) 2

c) 4

d) None of the mentioned


16) What will be the output of the following Java program?


interface calculate {
    void cal(int item);
}

class displayA implements calculate {
    int x;
    public void cal(int item) {
        x = item * item;
    }
}

class displayB implements calculate {
    int x;
    public void cal(int item) {
        x = item / item;
    }
}

class interfaces {
    public static void main(String args[]) {
        displayA arr1 = new displayA;
        displayB arr2 = new displayB;
        arr1.x = 0;
        arr2.x = 0;      
        arr1.cal(2);
        arr2.cal(2);
        System.out.print(arr1.x + " " + arr2.x);

    }
}

a) 0 0

b) 2 2

c) 4 1

d) 1 4


17) What will be the output of the following Java code?


class newthread extends Thread {
    newthread() {
        super("My Thread");
        start();
    }

    public void run() {
        System.out.println(this);
    }
}

class multithreaded_programing {
    public static void main(String args[]) {
        new newthread();
    }
}

a) My Thread

b) Thread[My Thread,5,main]

c) Compilation Error

d) Runtime Error


18. What will be the output of the following Java code?


class newthread extends Thread {
    Thread t;
    newthread() {
        t = new Thread(this, "My Thread");
        t.start();
    }

    public void run() {
            try {
                t.join()  
                System.out.println(t.getName());
            }

            catch(Exception e) {
                System.out.print("Exception");
            }
    }
}

class multithreaded_programing {
    public static void main(String args[]) {
        new newthread();
    }
}

a) My Thread

b) Thread[My Thread,5,main]

c) Exception

d) Runtime Error


19)  What is the name of the thread in output in the following Java program?


class multithreaded_programing {
    public static void main(String args[]) {

        Thread t = Thread.currentThread();
        System.out.println(t.getPriority());        

    }
}

a) 0

b) 1

c) 4

d) 5


20)  Which method is used to verify the actual and expected results in Junits?

a) assert()

b) equals()

c) ==

d) isEqual()


21) Find the output of the following code.


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

a. I

b. Compile Error 

c. Throws Exception

d. 24 I 

The correct answer is: 24 I 


22) 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. COMPILER ERROR

c. 10

d. EXCEPTION

The correct answer is: COMPILER ERROR


23) 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. 3

b. -127

c. 129

d. 127

The correct answer is: -127


24) Select the valid statement.


a. char[] ch = new char()

b. NONE OF THE ABOVE

c. char[] ch = new char(5)

d. char[] ch = new char[5]

The correct answer is: char[] ch = new char[5]


25) 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 14

b. 120 200

c. 120 200 016

d. 120 200 16

The correct answer is: 120 200 14


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

a. copy of the array

b. LENGTH OF ARRAY

c. NONE

d. The reference of the array

The correct answer is: The reference of the array


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

a. NONE

b. java.lang.String

c. java.lang.Object

d. java.lang.util

The correct answer is: java.lang.Object


27) 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. NONE

d. Replaces single occurrence of ‘s’ to ‘e’.

The correct answer is: Replaces all occurrences of ‘e’ to ‘s’.


28) Which of the following exception is thrown when divided by zero statement is executed?

a. NullPointerException

b. ArithmeticException

c. NumberFormatException

d. NONE

The correct answer is: ArithmeticException


29) Which of the following is used to find and fix bugs in the program?

a. JVM

b. JDK

c. JDB

The correct answer is: JDB




Post a Comment

0 Comments