Monday, May 13, 2024

java programming interview questions for 5 years experience

 Question-1. What will be the output of the following Java coding snippet?

import java.io.CharArrayReader;

import java.io.IOException;

class TestApp {

public static void main(String[] args) {

String obj = "abcdef";

int length = obj.length();

char c[] = new char[length];

obj.getChars(0, length, c, 0);

CharArrayReader io_1 = new CharArrayReader(c);

CharArrayReader io_2 = new CharArrayReader(c, 0, 3);

int i;

try {

while ((i = io_2.read()) != -1) {

System.out.print((char) i);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

A.abc

B.abcd

C.abcde

D.abcdef

Question-2. What would the following Java coding snippet return?

import java.io.CharArrayReader;

import java.io.IOException;

class TestApp {

public static void main(String[] args) {

String obj = "abcdef";

int length = obj.length();

char c[] = new char[length];

obj.getChars(0, length, c, 0);

CharArrayReader io_1 = new CharArrayReader(c);

CharArrayReader io_2 = new CharArrayReader(c, 1, 4);

int i, j;

try {

while ((i = io_1.read()) == (j = io_2.read())) {

System.out.print((char) i);

}

} catch (IOException e) {

e.printStackTrace();

}

}}

A.abc

B.abcd

C.abcde

D.abcdef

E.Nothing would get printed.

Question-3. What is the outcome of the below Java code?

class TestApp {

public static void main(String args[]) {

System.out.println(test());

}

static float test() {

static float x = 0.0;

return ++x;

}

}

A.0.0

B.1

C.1.0

D.Compile time error

Question-4. What does the following Java coding snippet yield?

class TestApp {

static int index = 0;

public static void main(String args[]) {

System.out.println(test());

}

int test() {

int index = 1;

return index;

}

}

A.0

B.1

C.Run-time error at line number 6

D.Compile time error

Question-5. Which of the following is the result of the below Java coding snippet?

class TestApp {

public static void main(String args[]) {

int bits;

bits = -3 >> 1;

bits = bits >>> 2;

bits = bits << 1;

System.out.println(bits);

}}

A.1

B.7

C.-2147483646

D.2147483646

Question-6. Which of the following is a result of the Java code given below?

class TestApp {

public static void main(String args[]) {

int index = 0;

boolean flag = true;

boolean reg1 = false, reg2;

reg2 = (flag | ((index++) == 0));

reg2 = (reg1 | ((index += 2) > 0));

System.out.println(index);

}

}

A.0

B.1

C.2

D.3

Question-7. What would the following Java coding snippet display on execution?

// Command - line: java TestApp 1 2 3 4 5

class TestApp {

public static void main(String[] args) {

System.out.println(args[1] + args[2] + args[3]);

}

}

A.1 2 3

B.123

C.234

D.Compilation Error

Question-8. What would the below Java coding snippet print if the input given is?

// Command - line: java TestApp abcqfghqbcd

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

class TestApp {

public static void main(String args[]) throws IOException {

char bit;

BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

do {

bit = (char) obj.read();

System.out.print(bit);} while (bit != 'q');

}

}

A.abcqfgh

B.abc

C.abcq

D.abcqfghq

Question-9. What would the following Java coding snippet yield on execution?

import java.io.File;

class TestApp {

public static void main(String args[]) {

File sys = new File("/java/system");

System.out.print(sys.canWrite());

System.out.print(" " + sys.canRead());

}

}

A.true false

B.false true

C.true true

D.false false

Question-10. What does the following Java coding snippet print as its output?

class Cluster {}

class Node1 extends Cluster {}

class Node2 extends Cluster {}

public class TestApp {

public static void main(String[] args) {

Cluster tree = new Node1();

if (tree instanceof Node1)

System.out.println("Node1");

else if (tree instanceof Cluster)

System.out.println("Cluster");

else if (tree instanceof Node2)

System.out.println("Node2");

else

System.out.println("Unexpected");

}

}

A.Cluster

B.Node1

C.Node2

D.Unexpected

Question-11. Which of the following is the result of the below program?

public class SimpleTest {public static void stringReplace(String str) {

str = str.replace('c', 'c');

}

public static void bufferReplace(StringBuffer str) {

str.trimToSize();

}

public static void main(String args[]) {

String myString = new String("cplus");

StringBuffer myBuffer = new StringBuffer(" plus");

stringReplace(myString);

bufferReplace(myBuffer);

System.out.println(myString + myBuffer);

}

}

A.cplusplus

B.plus plus

C.cplus plus

D.c plus plus

Question-12. Which of the following is the outcome of the below program?

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class SimpleTest {

public static void main(String args[]) throws IOException {

char bit;

BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

do {

bit = (char) console.read();

System.out.print(bit);

} while (bit != '\'');

}

}

A.abc’

B.abcdef/’

C.abc’def/’egh

D.abcqfghq

Question-13. Which of the following is the result of the below Java coding snippet?

import java.io.File;

public class SimpleTest {

public static void main(String args[]) {

File sys = new File("/MVC/system");

System.out.print(sys.getParent());

System.out.print(" " + sys.isFile());

}}

A.MVC true

B.MVC false

C.\MVC false

D.\MVC true

Question-14. Which of the following would the below Java coding snippet return on

execution?

public class SimpleTest {

static int test;

boolean final() {

test++;

return true;

}

public static void main(String[] args) {

test = 0;

if ((final() | final()) || final())

test++;

System.out.println(test);

}

}

A.1

B.2

C.3

D.Compilation error

Question-15. Which of the following values would the below Java coding snippet

yield?

public class SimpleTest {

public static void main(String[] args) {

String text = "199";

try {

text = text.concat(".5");

double decimal = Double.parseDouble(text);

text = Double.toString(decimal);

int status = (int) Math.ceil(Double.valueOf(text).doubleValue());

System.out.println(status);

} catch (NumberFormatException e) {

System.out.println("Invalid number");

}

}

}

A.199

B.199.5

C.200

D.Invalid numberQuestion-

16. Which of the following combinations would the below program print?

public class SimpleTest {

public static void main(String ags[]) {

String initial = "ABCDEFG", after = "";

after = initial = initial.replace('A', 'Z');

System.out.println(initial + ", " + after);

}

}

A.ABCDEFG, ABCDEFG

B.ABCDEFG, ZBCDEFG

C.ZBCDEFG, ABCDEFG

D.ZBCDEFG, ZBCDEFG

Question-17. Which of the following values would the below Java coding snippet

print?

public class SimpleTest {

public static void main(String args[]) {

String str = (String) returnStringAsArray()[-1 + 1 * 2];

System.out.println(str);

}

public static Object[] returnStringAsArray() {

return new String[] {"Java","Quiz"};

}

}

A.Java

B.ArrayIndexOutOfBoundsException

C.Quiz

D.Compilation error

Question-18. What would the below Java coding snippet print on execution?

public class SimpleTest {

public static void main(String args[]) {

try {

args[0] = "0";

return;

} catch (Exception e) {

System.out.print("Exception");

} finally {

System.out.print("Final");

}

}

}

A.Exception

B.Final

C.ExceptionFinal

D.Compilation error

Question-19. What does the following Java coding snippet print on execution?

public class SimpleTest {

public static void main(String[] args) {

int[] table = {1,2,3,4,5};

table[1] = (table[2 * 1] == 2 - args.length) ? table[3] : 99;

System.out.println(table[1]);

}

}

A.Compilation fails.

B.3

C.2

D.99

Question-20. What would be the output of the below Java coding snippet upon

execution?

import java.util.Random;

public class SimpleTest {

static int count = 0;

public static void main(String[] args) throws InterruptedException {

Consumer test = new Consumer();

Producer prod1 = new Producer(test, "thread-1");

Producer prod2 = new Producer(test, "thread-2");

prod1.start();

prod2.start();

}

}

class Producer extends Thread {

Consumer test;

String message;

Producer(Consumer test, String msg) {

this.test = test;

message = msg;

}

public void run() {

Random rand = new Random();

int randomNum = rand.nextInt((1000 - 10) + 1) + 10;

System.out.println(message);

}

}

class Consumer {

private int count = 0;

public int nextCounter() {

synchronized(this) {count++;

return count;

}

}

}

A.Runtime Exception

B.thread-1 thread-2

C.thread-2 thread-1

D.Sometimes thread-2 will precede thread-1

No comments:

Post a Comment