0% found this document useful (0 votes)
83 views17 pages

Mock Test 1

This document contains a mock test with 13 multiple choice questions about Java programming concepts. It provides the question, possible answer choices, and indicates the correct answer with a check mark. The questions cover topics like strings, arrays, exceptions, interfaces, and more. The purpose is to test understanding of Java syntax, semantics, and best practices.

Uploaded by

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

Mock Test 1

This document contains a mock test with 13 multiple choice questions about Java programming concepts. It provides the question, possible answer choices, and indicates the correct answer with a check mark. The questions cover topics like strings, arrays, exceptions, interfaces, and more. The purpose is to test understanding of Java syntax, semantics, and best practices.

Uploaded by

mohammed karam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

CT038-3-2-OODJ-L-2
Mock Test

Points: 18/30


1. Given the following code, which expression displays the word "Equal"?

String s1="Java";
String s2="java";
if(expression) {
System.out.println("Equal");
} else {
System.out.println("Not equal");
}*
(0/1 Points)

s1==s2

s1.matchCase(s2)

s1.equalsIgnoreCase(s2) 
s1.equals(s2)

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 1/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

2. Which of the following statements can be used to determine if cat can be


found in the list?

ArrayList<String> list = new ArrayList<>();


list.add("dog");
list.add("cat");
list.add("frog"); *
(1/1 Points)

list.contains("cat") 
list.hasObject("cat")

list.indexOf(1)

list.indexOf(2)

3. Given the code fragment:

// Line n1
switch (cardVal) {
case 4: case 5: case 6:
case 7: case 8:
System.out.println("Hit");
break;
case 9: case 10: case 11:
System.out.println("Double");
break;
case 15: case 16:
System.out.println("Surrender");
break;
default:
System.out.println("Stand");
}

Which code fragment can be inserted at Line n1, independently, enable to print
Stand? *
(1/1 Points)

int cardVal = 8;

int cardVal = 10;

int cardVal = 14; 


int cardVal = 16;
https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 2/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

4. Select the incorrect method declaration to be inserted at //INSERT CODE HERE:

interface Printable {
void print();
}
class Square implements Printable {
public void print() {
System.out.println("Square print");
}
}
class Rectangle implements Printable {
public void print() {
System.out.println("Rectangle print");
}
}
public class Test {
// INSERT CODE HERE
movable.print();
}
}*
(1/1 Points)

void walk(Printable movable) {

void walk(Square movable) {

void walk(Rectangle movable) {

void walk() { 

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 3/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

5. What will happen when you compile and run the following code?

public class Main{


private int i = 1;
public static void main(String argv[]){
int i = 2;
Main s = new Main ();
s.someMethod();
}
public static void someMethod(){
System.out.println(i);
}
}*
(1/1 Points)

1 will be printed out

2 will be printed out

A compile time error will be generated 


An exception will be thrown

6. What's the output of the following code?

public class Main {


public static void main(String[] args) {
int i = 10;
do
while (i < 15)
i = i + 20;
while (i < 2);
System.out.println(i);
}
}*
(1/1 Points)

10

30 
31

32

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 4/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

7. Given the code fragment:

public class Test {


public static void main(String[] args) {
int x = 10;
int y = 2;
try {
for (int z = 2; z >= 0; z--) {
int ans = x / z;
System.out.print(ans+ " ");
}
} catch (Exception e1) {
System.out.println("E1");
} catch (ArithmeticException e1) {
System.out.println("E2");
}
}
}

What is the result? *


(1/1 Points)

E1

E2

5 10 E1

Compilation fails. 

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 5/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

8. What will be printed out when the following code is executed?

switch (5) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
default:
System.out.println("default");
case 2:
System.out.println("two");
}*
(0/1 Points)

one

default and two 


one, two, and default

Nothing, a compile-time error is generated

9. Which statement will display each element of the array?

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


(1/1 Points)

for(int n : myArray[]) { System.out.println(n); }

for(int n : myArray) { System.out.println(n); } 


for(int n=1; n < 5; n++) { System.out.println(myArray[n]); }

for(int n=1; n <= 4; n++) { System.out.println(myArray[n]); }

10. Which of the following expressions will evaluate to 7? *


(1/1 Points)

2 + 4 * 3- 7 
(2 + 4) * (3 - 7)

2 + (4 * (3 – 7))
https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 6/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

((2 + 4) * 3) - 7)

11. What is the output of the following code?

class Main {
public static void main(String args[]) {
String s = "java2s";
s.replace('a', 'Z').trim().concat("Aa");
s.substring(0, 2);
System.out.println(s);
}
}*
(1/1 Points)

javZ2sAa

jZvZ2sAa

jZvZ2s

java2s 

12. Which of the following declares an array that supports two rows and a variable
number of columns? *
(1/1 Points)

int myArray[][] = new int[2][3];

int myArray[][] = new int[2][]; 


int myArray[][] = new int[][];

int myArray[][] = new int[][3];

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 7/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

13. Given:

public interface MyInt {


public void method1() {
System.out.println("method1");
}
public default void method2() {
System.out.println("method2");
}
public static void method3() {
System.out.println("method3");
}
public abstract void method4();
}

Which statement is true? *


(0/1 Points)

Only method4() compiles

Only method2() and method4() compiles.

Only method2(), method3(), and method4() compiles. 


MyInt.java compiles.

14. Given the code fragment:

public class App{


public static void main(String[] args) {
String[] fruits = {"banana", "apple", "pears", "grapes"};
Arrays.sort(fruits, (a, b) -> a.compareTo(b));
for (String s : fruits) {
System.out.print(" "+s);
}
}
}

What is the result? *


(1/1 Points)

apple banana grapes pears 


pears grapes banana apple

banana apple pears grapes


https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 8/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

Compilation fails.

15. Given:

abstract class Writer {


public static void write() {
System.out.println("Writing...");
}
}
class Author extends Writer {
public static void write() {
System.out.println("Writing book");
}
}
public class Programmer extends Writer {
public static void write() {
System.out.println("Writing code");
}
public static void main(String[] args) {
Writer w = new Programmer();
w.write();
}
}

What is the result? *


(1/1 Points)

Writing... 
Writing book

Writing code

Compilation fails.


16. Which of the following lines will compile without warning or error? *
(0/1 Points)

char d="d";

float f=3.1415;

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 9/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

int i=34; 
byte b=257;


17. Given:

import java.util.ArrayList;
import java.util.List;
public class JavaSETest {
public static void main(String[] args) {
List<Integer> elements = new ArrayList<>();
elements.add(10);
int firstElmnt = elements.get(1);
System.out.println(firstElmnt);
}
}

What is the result? *


(0/1 Points)

null

10

An IndexOutOfBoundsException is thrown at runtime. 

18. Which access modifier makes a member available only to classes within the
same package or subclasses? *
(1/1 Points)

private

protected 
public

package-private

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 10/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

19. Which of the following statement will not compile?

int i = 5;
int j = 10; *
(1/1 Points)

while(i < j) {}

while(i) {} 
while((i = 12)==5) {}

while((i = 12)!=5) {}


20. Given the code fragment:

StringBuilder s1 = new StringBuilder("Java");


String s2 = "Love";
s1.append(s2);
s1.substring(4);
int foundAt = s1.indexOf(s2);
System.out.println(foundAt);

What is the result? *


(0/1 Points)

-1

4 
A StringIndexOutOfBoundsException is thrown at runtime.

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 11/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

21. Which of the following lines of code cannot individually replace the //INSERT
CODE HERE line so that the code compiles successfully?

public class Main {


public static int getVal() {
return 100;
}
public static void main(String args[]) {
int num = 10;
final int num2 = 20;
switch (num) {
// INSERT CODE HERE
break;
default: System.out.println("default");
}
}
}*
(0/1 Points)

case 10*3: System.out.println(2);

case num: System.out.println(3); 


case 10/3: System.out.println(4);

case num2: System.out.println(5);

22. Which of the following statements will compile without an error? *


(1/1 Points)

int myArray[5];

int myArray[5] = new int[5];

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

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

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 12/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

23. Given:

class SuperClass {
SuperClass(int x) {
System.out.println("Super");
}
}
public class SubClass extends SuperClass {
SubClass() {
// Line n1
System.out.println("Sub 2");
}
}

Which statement, when inserted at Line n1, enables the code to compile? *
(1/1 Points)

this(10);

super(10); 
SuperClass(10);

super.SuperClass (10);

24. Which of the following will return the position of the first letter a where the
string s contains "banana"? *
(1/1 Points)

lastIndexOf(2,s);

s.indexOf('a'); 
s.charAt(2);

indexOf(s,'v');

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 13/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

25. What's the output of the following code?

public class Main {


public static void main(String args[]) {
int i = 0;
for (; i < 2; i = i + 5) {
if (i < 5)
continue;
System.out.println(i);
}
System.out.println(i);
}
}*
(0/1 Points)

Compilation error

5 
10

26. Given the code fragment:

public class TestClass {


public static void main(String[] args) {
List<String> items = new ArrayList<>();
items.add("Pen");
items.add("Pencil");
items.add("Box");
for (String i : items) {
if (i.indexOf("P") == 0) {
continue;
} else {
System.out.print(i+" ");
}
}
}
}

What is the result? *


(1/1 Points)

Pen Pencil Box

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 14/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

Pen Pencil

Box 
Compilation fails.


27. Which of the following statements are correct if they replace the comment line?

public class Main{


public int i;
public static void main(String argv[]){
Main sc = new Main();
// Comment line
}
}*
(0/1 Points)

System.out.println(i);

System.out.println(sc.i); 
System.out.println(Main.i);

System.out.println((Main()).i);


28. What is the result of the following operation?

System.out.println(4^3); *
(0/1 Points)

7 
12

64

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 15/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test


29. Which of the following statements will compile with syntax or runtime errors if
they replace the comment line?

public class Main{


public int i;
public static void main(String argv[]){
Main sc = new Main();
// Comment line
}
}*
(0/1 Points)

sc.i = 5;

int j = sc.i;

sc.i = 5.0; 
System.out.println(sc.i);


30. What's the output of the following code?

public class Main {


public static void main(String args[]) {
int num = 0;
switch (num) {
default:
System.out.println("default");
break;
case 0:
System.out.println("case1");
break;
case 20 - 10 * 2:
System.out.println("case2");
break;
}
}
}*
(0/1 Points)

default

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 16/17
10/6/2020 CT038-3-2-OODJ-L-2 Mock Test

case1

case2

Compilation error 

Go back to thank you page

This content is created by the owner of the form. The data you submit will be sent to the form owner. Never give out
your password.

Powered by Microsoft Forms | Privacy and cookies | Terms of use

https://forms.office.com/Pages/ResponsePage.aspx?id=owPtDy1AM0aozYswiCIlPnKy_4jH5-tDkTVRGyORkGBUNVFEWExNTUJTSzE0QU8w… 17/17

You might also like