Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz 5

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

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

for(int i = 0; i < a.length; i++) {

a[i] = a[(a.length - 1) - i];

}

System.out.print(a[1]);

a)

2

b)

3

c)

1

d)

4

2.

int[] a = {5, 10, 15};

int sum = 0;

for(int i = 0; i < a.length; i++) {

sum += a[i] / a[0];

}

System.out.print(sum);

a)

6

b)

7

c)

8

d)

30

3.

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

for(int i = 1; i < a.length; i++) {

a[i] = a[i] + a[i-1];

}

System.out.print(a[3]);

a)

8

b)

9

c)

10

d)

11

4.

int[] a = {2, 4, 6};

for(int i = 0; i < a.length; i++) {

a[i] = a[i] * i;

}

System.out.print(a[2]);

a)

0

b)

4

c)

6

d)

12

5.

int[][] a = {

{1, 2, 3},

{4, 5, 6}

};

System.out.print(a.length + a[0].length);

a)

3

b)

4

c)

5

d)

6

6.

int[][] a = {

{2, 4},

{6, 8}

};

for(int i = 0; i < a.length; i++) {

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

if(a[i][j] % 4 == 0)

a[i][j] /= 2;

}

}

System.out.print(a[1][1]);

a)

4

b)

8

c)

6

d)

2

7.

int[] a = {2, 4, 6, 8};

int x = a[0];

for(int i = 1; i < a.length; i++) {

x = x + a[i] - i;

}

System.out.print(x);

a)

13

b)

14

c)

15

d)

16

8.

String s = "abcde";

System.out.print(s.substring(1,4));

a)

bcde

b)

bcd

c)

abcd

d)

abc

9.

String s = "A" + 10 + 20;

System.out.print(s);

a)

A30

b)

A1020

c)

A2030

d)

Compilation error

10.

String s = "java";

s = s.replace('a', 'o');

System.out.print(s);

a)

jovo

b)

jova

c)

java

d)

jav

11.

String s = "abcd";

s.replace("ab", "xy");

System.out.print(s.substring(1, 3));

a)

xy

b)

yc

c)

bc

d)

abxy

12.

String s = "abc";

System.out.print(s.equals(s.toUpperCase().toLowerCase()));

a)

true

b)

false

c)

error

d)

ABCabc

13.

static void fun(String s) {

s = s + "X";

}

public static void main(String[] args) {

String s = "A";

fun(s);

s += "Y";

System.out.print(s);

}

a)

AX

b)

AY

c)

AXY

d)

A

14.

StringBuilder sb = new StringBuilder("abc");

sb.reverse().append("x").reverse();

System.out.print(sb);

a)

abcx

b)

cbax

c)

xabc

d)

error

15.

String s = "1a2b3c";

s = s.replaceAll("\\d", "");

System.out.print(s.length());

a)

6

b)

5

c)

4

d)

3

16.

String s = "HELLO";

s.toLowerCase();

System.out.println(s);

a)

HELLO

b)

hello

c)

hELLO

d)

Hello

17.

String s = "banana";

System.out.println(s.indexOf("na", 3));

a)

2

b)

3

c)

4

d)

-1

18.

StringBuilder sb = new StringBuilder("abc");

String s = sb.toString();

sb.append("d");

System.out.println(s);

a)

abc

b)

abcd

c)

error

d)

d

19.

static void fun(String s, int i) {

if(i == s.length()) return;

fun(s, i + 1);

System.out.print(s.charAt(i));

}

public static void main(String[] args) {

fun("abcd", 0);

}

a)

0

b)

abcd

c)

dcba

d)

a

20.

static String solve(String s) {

if(s.length() <= 1) return s;

return solve(s.substring(1)) + s.charAt(0);

}

public static void main(String[] args) {

System.out.println(solve("java"));

}

a)

avaj

b)

java

c)

j

d)

a