0% found this document useful (0 votes)
16 views24 pages

Java String and Array Output Questions

The document contains a series of technical questions related to Java programming, specifically focusing on string manipulation, array handling, and object references. Each question presents code snippets and multiple-choice answers to test the reader's understanding of Java concepts. The questions cover various topics including string comparison, array operations, and the behavior of Java's built-in methods.

Uploaded by

ppop57654
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views24 pages

Java String and Array Output Questions

The document contains a series of technical questions related to Java programming, specifically focusing on string manipulation, array handling, and object references. Each question presents code snippets and multiple-choice answers to test the reader's understanding of Java concepts. The questions cover various topics including string comparison, array operations, and the behavior of Java's built-in methods.

Uploaded by

ppop57654
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Some sample Question for ES-III (technical portion)

Q1. What is the output of the following Java code?

public class Test {

public static void main(String[] args) {

String a = "ab";

String b = "a" + "b";

String c = new String("ab");

[Link]((a == b) + " " + (a == c) + " " + [Link](c));

A) true true true

B) true false true

C) false false true

D) true false false

Q2. What is the output?

public class Test {

public static void main(String[] args) {

String x = "hello";

[Link](" world");

[Link](x);

A) hello world

B) hello

C) helloworld

D) Compilation error
Q3. What is printed?

public class Test {

public static void main(String[] args) {

String s1 = "Java";

String s2 = [Link]();

String s3 = [Link]();

[Link]((s1 == s2) + " " + (s1 == s3));

A) false false

B) false true

C) true false

D) true true

Q4. Which statement about the string pool is true?

A) Calling new String("abc").intern() returns a different reference than the pooled "abc"

B) [Link]() guarantees reference equality with a pooled instance for equal content

C) String literals are not stored in the pool after Java 9

D) intern() mutates the original string to become pooled

Q5. What happens at runtime?

public class Test {

public static void main(String[] args) {

String s = null;

[Link](s + 10);

A) Prints 10

B) Prints null10

C) Throws NullPointerException

D) Compilation error
Q6. What is the output?

public class Test {

public static void main(String[] args) {

String a = "x";

String b = "x";

String c = a + b;

String d = "xx";

[Link](c == d);

A) true

B) false

C) Compilation error

D) null

Q7. What is printed?

public class Test {

public static void main(String[] args) {

final String A = "foo";

String B = "foo";

String s1 = A + "bar";

String s2 = B + "bar";

String s3 = "foobar";

[Link]((s1 == s3) + " " + (s2 == s3));

A) true true

B) true false

C) false true

D) false false
Q8. What is the result?

public class Test {

public static void main(String[] args) {

String s = "abc";

[Link]('a', 'a').replace("b", "b");

[Link](s);

A) abc

B) aabc

C) abcb

D) Compilation error

Q9. What happens?

public class Test {

public static void main(String[] args) {

String s = "012345";

[Link]([Link](2, 2).isEmpty() + " " + [Link](6));

A) true followed by empty line

B) false (two spaces)

C) true then throws StringIndexOutOfBoundsException

D) true followed by "" (empty string printed)

Q10. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {


String[] parts = "a,,b,".split(",", -1);

[Link]([Link](parts));

A) [a, , b]

B) [a, , b, ]

C) [a, , b, , ]

D) [a,,b,]

Q11. What is printed?

public class Test {

public static void main(String[] args) {

String s = " \u2003Hello\u00A0";

[Link]("[" + [Link]() + "] [" + [Link]() + "]");

A) [Hello] [Hello]

B) [ Hello ] [Hello] (spaces preserved in first, removed in second)

C) [Hello] [ Hello ]

D) [ Hello ] [ Hello ]

Q12. Predict the output:

public class Test {

public static void main(String[] args) {

StringBuilder sb = new StringBuilder("X");

for (int i = 0; i < 3; i++) {

[Link](i);

String s = [Link]();

[Link]("12", "AB");

[Link](sb + " " + s);


}

A) X012 X0AB

B) X012 X012

C) X0AB X0AB

D) X012 X0ABAB

Q13. What is the output?

public class Test {

public static void main(String[] args) {

String a = "A";

String b = "B";

[Link](1 + 2 + a + b + (3 + 4));

A) 3AB7

B) 12AB34

C) 3AB34

D) 3AB then 7 on next line

Q14. What happens?

public class Test {

public static void main(String[] args) {

[Link]([Link]((Object)null) + "-" + [Link](null));

A) Prints null-null

B) Throws NullPointerException

C) Prints null- (empty after dash)

D) Compilation error
Q15. What is the output?

public class Test {

public static void main(String[] args) {

String s = "banana";

[Link]([Link]("ana", "X") + " " + [Link]("ana", "X"));

A) bXa X

B) bXa bXa

C) bXa Xna

D) baX X

Q16. What is the output?

public class Test {

public static void main(String[] args) {

String s = "🙂🙂"; // two emojis (surrogate pairs)

[Link]([Link]() + " " + [Link](0, [Link]()));

A) 2 2

B) 4 2

C) 2 4

D) 4 4

Q17. What happens?

import [Link];

public class Test {

public static void main(String[] args) {

StringJoiner sj = new StringJoiner(",", "[", "]");

[Link]("a").add(null).add("c");
[Link]([Link]());

A) Prints [a,null,c]

B) Throws NullPointerException

C) Prints [a,,c]

D) Compilation error

Q18. Which statement about StringBuilder vs StringBuffer is correct?

A) Both are immutable and thread-safe

B) StringBuilder is mutable and not synchronized; StringBuffer is mutable and synchronized

C) StringBuilder is synchronized; StringBuffer is not

D) Both are immutable and not thread-safe

Q19. What is printed?

public class Test {

public static void main(String[] args) {

String s1 = "abc";

String s2 = [Link]();

String s3 = new String("abc").intern();

[Link]((s1 == s2) + " " + (s1 == s3));

A) true true

B) true false

C) false true

D) false false

Q20. What is printed?

public class Test {

public static void main(String[] args) {


String s = "abc";

[Link]([Link]("abd") + " " + [Link]("ABC"));

A) -1 0

B) 1 0

C) -1 -1

D) 0 1

Q21. What is the output of the following Java code?

public class Test {

public static void main(String[] args) {

int[] a = new int[3]; // {0,0,0}

a[1] = a[0]++ + ++a[2]; // a[0] used (0) then becomes 1; a[2] becomes 1 then used

[Link](a[0] + " " + a[1] + " " + a[2]);

A) 0 1 1

B) 1 1 1

C) 1 1 0

D) 1 0 1

Q22. What happens at runtime?

public class Test {

public static void main(String[] args) {

Number[] arr = new Integer[2];

arr[0] = 42; // OK (auto-boxes to Integer)

arr[1] = 3.14; // ?

A) Both assignments succeed


B) ArrayStoreException at arr[1] = 3.14

C) ClassCastException at arr[1] = 3.14

D) Compilation error

Q23. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {

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

[Link](src, 1, src, 2, 3); // overlapping copy within same array

[Link]([Link](src));

A) [1,2,2,3,4]

B) [1,1,2,3,4]

C) [1,2,3,4,5]

D) [1,2,3,3,3]

Q24. What is the output?

public class Test {

public static void main(String[] args) {

int[][] m = new int[2][];

m[0] = new int[]{10, 20};

[Link]([Link] + " " + m[0].length + " " + (m[1] == null));

A) 2 2 false

B) 2 2 true

C) 2 0 true

D) Throws NullPointerException
Q25. What is printed?

import [Link];

public class Test {

public static void main(String[] args) {

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

int pos = [Link](a, 3);

[Link](pos);

A) Index of 3 (which is 3)

B) A negative number (unspecified)

C) -1

D) Compilation error

Q26. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {

int[] a = {1, 2, 3};

int[] b = [Link]();

b[1] = 99;

[Link](a[1] + " " + b[1] + " " + (a == b));

A) 2 99 true

B) 2 99 false

C) 99 99 false

D) 99 2 false
Q27. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {

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

for (int x : a) { x *= 2; }

[Link]([Link](a));

A) [2, 4, 6, 8]

B) [1, 2, 3, 4]

C) [2, 2, 3, 4]

D) Compilation error

Q28. What is printed?

import [Link];

public class Test {

public static void main(String[] args) {

int[] a = {1,2,3};

[Link](a); // line 1

[Link]([Link](a)); // line 2

A) Line1: contents; Line2: hash code

B) Line1: [1, 2, 3]; Line2: [1, 2, 3]

C) Line1: something like [I@1b6d3586; Line2: [1, 2, 3]

D) Both lines print object identity strings

Q29. What happens?


import [Link];

public class Test {

public static void main(String[] args) {

int[] a = {1,2,3};

int[] b = [Link](a, 5);

[Link]([Link](b));

A) [1, 2, 3, 0, 0]

B) [1, 2, 3]

C) [1, 2, 3, null, null]

D) Throws ArrayIndexOutOfBoundsException

Q30. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {

Integer[] box = new Integer[3];

[Link](box, new Integer(7));

box[0] = 99;

[Link]([Link](box));

A) [99, 99, 99]

B) [99, 7, 7]

C) [7, 7, 7]

D) [7, 99, 7]

Q31. What happens at runtime?


public class Test {

public static void main(String[] args) {

int[] a = new int[-1];

[Link]([Link]);

A) Prints -1

B) Throws NegativeArraySizeException

C) Throws ArrayIndexOutOfBoundsException

D) Compilation error

Q32. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {

double[] d = {0.0, -0.0, [Link]};

[Link](d);

[Link]([Link](d));

A) [0.0, -0.0, NaN]

B) [-0.0, 0.0, NaN]

C) [NaN, -0.0, 0.0]

D) Order is undefined

Q33. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {


String[] a = {"x","y"};

String[] b = {"x","y"};

[Link]([Link](a,b) + " " + [Link](new Object[]{a}, new Object[]


{b}));

A) true false

B) true true

C) false true

D) false false

Q34. What happens?

import [Link];

import [Link];

public class Test {

public static void main(String[] args) {

int[] a = {1,2,3};

List<int[]> list = [Link](a);

[Link]([Link]() + " " + [Link](0)[1]);

A) 3 2

B) 1 2

C) 3 1

D) 1 1

Q35. What is the output?

import [Link];

public class Test {


public static void main(String[] args) {

Integer[] a = {1,2,3};

var list = [Link](a); // fixed-size list backed by the array

[Link](1, 99);

[Link]([Link](a) + " " + [Link]());

A) [1, 2, 3] 3

B) [1, 99, 3] 3

C) [1, 99, 3] 2

D) Throws UnsupportedOperationException

Q36. What is the result?

public class Test {

static void f(int[] x) { x[0] = 42; x = new int[]{7,7,7}; }

public static void main(String[] args) {

int[] a = {1,2,3};

f(a);

[Link](a[0] + " " + [Link]);

A) 42 3

B) 7 3

C) 7 1

D) 1 3

Q37. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {


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

int idx = [Link](a, new int[]{1,2,0,4});

[Link](idx);

A) -1

B) 2

C) 3

D) 1

Q38. What is the output?

public class Test {

public static void main(String[] args) {

[Link](g(null));

static String g(int[] a) { return "int[]"; }

static String g(String[] a) { return "String[]"; }

A) int[]

B) String[]

C) Compilation error: reference to g is ambiguous

D) null

Q39. What is the output?

import [Link];

public class Test {

public static void main(String[] args) {

Object[] o = new Object[3];

StringBuilder sb = new StringBuilder("x");

[Link](o, sb);
[Link]("y");

[Link]([Link](o));

A) [x, x, x]

B) [xy, xy, xy]

C) [x, xy, x]

D) Throws NullPointerException

Q40. What happens?

public class Test {

public static void main(String[] args) {

int[][] grid = new int[2][];

for (int i = 0; i < [Link]; i++) {

// intentionally not allocating grid[i] when i==1

if (i == 0) grid[i] = new int[1];

grid[i][0] = i + 10; // ?

[Link](grid[0][0]);

A) Prints 10

B) Prints 11

C) Throws NullPointerException

D) Compilation error

Q41. What is the output of the following Java code?

public class Test {

public static void main(String[] args) {

int x = 10;

if (x > 5)
if (x % 2 == 0)

x += 3;

else

x += 5;

[Link](x);

A) 13

B) 15

C) 10

D) 8

Q42. What happens at runtime?

public class Test {

public static void main(String[] args) {

String s = null;

switch (s) {

case "OK": [Link](1); break;

default: [Link](0);

A) Prints 0

B) Prints 1

C) Throws NullPointerException

D) Compilation error

Q43. What is the output?

public class Test {

public static void main(String[] args) {

int sum = 0;
for (int i = 0; i < 5; i++) {

if (i == 2) continue; // skip 2

if (i == 4) break; // stop before 4

sum += i; // add 0,1,3 = 4

sum += 2; // ensure final becomes 6 for this crafted MCQ

[Link](sum);

A) 3

B) 6

C) 7

D) 10

Q44. What is the output?

public class Test {

public static void main(String[] args) {

int outer = 0, inner = 0;

OUTER:

for (int i = 1; i <= 2; i++) {

for (int j = 1; j <= 3; j++) {

if (j == 3) break OUTER; // break when j hits 3 first time

inner++; // executes for j=1,2 at i=1

outer++; // never reached due to break OUTER

[Link](outer + " " + inner);

A) 0 3

B) 1 3
C) 0 2

D) 1 2

Q45. What is printed?

public class Test {

public static void main(String[] args) {

int i = 0;

do {

[Link](i);

} while (i++ < 0);

A) 0

B) nothing printed

C) 01

D) 0 then 1 on next line

Q46. What is the output?

public class Test {

public static void main(String[] args) {

int a = 0, b = 0;

if (condA() && (++a > 0) & condB() || (b++ == 0)) {

// empty

[Link](a + " " + b);

static boolean condA() { return false; }

static boolean condB() { return true; }

A) 0 1

B) 1 0

C) 0 0
D) 1 1

Q47. What is the output?

public class Test {

static int f() {

int x = 5;

try {

return x++; // return value computed as 5, x becomes 6

} finally {

x++; // x becomes 7, but return value already determined

[Link](x); // prints 7 first

public static void main(String[] args) {

[Link](f());

A) 57

B) 75

C) 7

D) 5

Q48. What is printed?

public class Test {

public static void main(String[] args) {

int day = 2;

switch (day) {

default: [Link]("X");

case 1: [Link]("A");

case 2: [Link]("B");

break;
case 3: [Link]("C");

A) XB

B) AB

C) XAB

D) B

Q49. What is the output?

public class Test {

public static void main(String[] args) {

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

int count = 0;

ROWS:

for (int i = 0; i < [Link]; i++) {

for (int j = 0; j < m[i].length; j++) {

if (m[i][j] % 2 == 0) continue; // skip evens

if (m[i][j] > 4) continue ROWS; // go to next row

count++;

[Link](count);

A) 2

B) 3

C) 4

D) 1

Q50. What is the output?


public class Test {

public static void main(String[] args) {

Object obj = true ? 1 : 1L; // conditional operator with numeric operands

[Link]([Link]().getName());

A) [Link]

B) [Link]

C) [Link]

D) Compilation error

Long Questions
--programs based on searching
program based on sorting(ex:selection,bubble,insertion etc)
programs based on strings

You might also like