wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

CS0007 - Diagnostic Exam

Total questions: 50

Worksheet time: 25mins

Name
Class
Date
1.

Which tool is used to interpret a Java program?

a)

javac

b)

JDK

c)

JVM

d)

JRE

2.

What does the Java compiler do?

a)

Compiles source code into byte code

b)

Compiles source code into machine code

c)

Compiles byte code into machine code

d)

Compiles byte code into source code

3.

What does the Java interpreter do?

a)

Interprets source code into byte code

b)

Interprets source code into machine code

c)

Interprets byte code into machine code

d)

Interprets byte code into source code

4.

Given the program: public class Test { } What should be the complete file name when saving the given Java program?

a)

Test.class

b)

Test.jar

c)

Test.txt

d)

Test.java

5.

What will happen when you compile and run the following code? int year = 2004; switch(year) { case 2001: case 2002: case 2003: case 2004: case 2005: System.out.print("Normal Year"); break; case 2004: System.out.print("Leap Year"); }

a)

Normal Year

b)

Leap Year

c)

Compilation Error

d)

Runtime Error

6.

With the statement, int x = -((32<<2) ^ (520>>2)); What is the value of x?

a)

-2089

b)

2088

c)

2

d)

-3

7.

Which statement is NOT equal to ++x?

a)

x++

b)

x=1+x

c)

x=+1

d)

x+=1

8.

Given the snippet codes, int x=0, y=10; for(x=0; x<10; x++) { for(y=10; y>x; y--) { if(y==x) break; } } System.out.println(y); What is the printed value of variable y?

a)

a. 9

b)

b. 10

c)

c. No output due to syntax error.

d)

d. No output due to infinite loop.

9.

A junior developer is trying to shade every other row in a table. Odd-numbered rows should be silver. Even-numbered rows should be white. Which missing code is needed to satisfy the requirements? public class BackgroundShading { public static void main(String[] args) { String bgColor; for(int i = 0; i < 20; i++) { if ( ______ ) { bgColor="silver"; } else { bgColor="white"; } } } }

a)

i % 2 == 0

b)

i / 2 == 0

c)

i % 2 == 1

d)

i / 2 == 0

e)

i == 2

10.

With the program: class Test { public void message() {} int message(String msg) { return msg; } } What OOP concept is shown in the given program?

a)

encapsulation

b)

inheritance

c)

polymorphism

d)

abstraction

11.

Which variable is accessible within the class A?

a)

w and z

b)

w, z and x

c)

all except Y

d)

All of the variables

12.

Which is an instance variable?

a)

z

b)

Y

c)

w and x

d)

w, x and Y

13.

Which is a class variable?

a)

z

b)

Y

c)

w and x

d)

w, x and Y

14.

Which method is automatically executed when creating an object?

a)

main method

b)

standard method

c)

custom method

d)

constructor method

15.

Which statement will display the speed attribute of the class Car in method main()?

a)

mazda.speed

b)

this.speed

c)

super.speed

d)

bmw.speed

16.

Which statement will display the speed attribute of the class SportsCar in method main()?

a)

this.speed

b)

bmw.speed

c)

mazda.speed

d)

super.speed

17.

Which statement is true regarding speed variable?

a)

The variable speed has been overridden in class SportsCar.

b)

The variable speed of Car can’t be inherited by SportsCar due to encapsulation.

c)

The variable speed has been hidden from class SportsCar since Java doesn’t support overloading of variables.

d)

The variable speed has been overloaded in class SportsCar.

18.

Which variable will require a standard method in Car?

a)

speed

b)

price

c)

model

d)

brand

19.

Which is true about the SportsCar? (Choose the best answer)

a)

The SportsCar will inherit all the non-private attributes and operations of its parent class.

b)

The SportsCar will inherit all the non-private attributes and operations of its based class and its ancestors.

c)

SportsCar will inherit all the class attributes and operations of its super class.

d)

SportsCar will inherit all the attributes and operations of its parent class and its ancestors.

20.

It is used to forcibly or manually throw an exception.

a)

try

b)

catch

c)

throw

d)

throws

21.

What will be the output of the following program? class X { int method(int i) { return i *= i; } } class Y extends X { double method(double d) { return d /= d; } } class Z extends Y { float method(float f) { return f += f; } } public class MainClass { public static void main(String[] args) { Z z = new Z(); System.out.println(z.method(21.12)); } }

a)

3.0

b)

2.0

c)

4.0

d)

1.0

22.

In using the class FileInputStream, what is the return value of read() method when the end of file has been reached?

a)

null

b)

Boolean value

c)

-1

d)

0

23.

Which of the following correctly initializes a FileInputStream object for reading the file "test.txt"?

a)

FileOutputStream fis = new FileOutputStream("test.txt");

b)

FileOutputStream fis = new FileOutputStream(test.txt);

c)

FileInputStream fis = new FileInputStream("test.txt");

d)

FileInputStream fis = new FileInputStream(test.txt);

24.

What condition should be used in the while loop to ensure the file is read until the end?

a)

fis.read() != -1

b)

(value=fis.read()) != -1

c)

(value=fis.read()) != null

d)

fis.read() != null

25.

Which is a standard output stream in Java?

a)

FileOutputStream

b)

System.out

c)

FileWriter

d)

DataOutputStream

26.

HashLinkedSet class represents the LinkedList implementation of Set Interface.

a)

True

b)

False

27.

SortedSet is the alternate of Set interface that provides a total ordering on its elements.

a)

True

b)

False

28.

It's called a stream because it is like a stream of water that continues to flow.

a)

True

b)

False

29.

OutputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes.

a)

True

b)

False

30.

The iterable interface is the root interface for all the collection classes.

a)

True

b)

False

31.

Which method is used to read a single byte from a FileInputStream?

a)

fetchByte()

b)

read()

c)

readByte()

d)

getByte()

32.

What is the default access modifier for class members in Java?

a)

public

b)

package-private

c)

protected

d)

private

33.

Which of the following is a valid way to create an instance of a String in Java?

a)

String str = 'Hello';

b)

String str = new String();

c)

String str = String();

d)

String str = "Hello";

34.

What does the keyword 'static' signify in a method declaration?

a)

The method can be overridden

b)

The method belongs to the class rather than instances

c)

The method is private

d)

The method is synchronized

35.

What is the default value of a boolean variable in Java?

a)

true

b)

false

c)

1

d)

0

36.

Which of the following is not a valid Java identifier?

a)

myVariable

b)

$myVariable

c)

_myVariable

d)

2ndVariable

37.

What keyword is used to create a subclass in Java?

a)

extends

b)

implements

c)

super

d)

inherits

38.

Which of the following methods can be used to convert a string to an integer in Java?

a)

Integer.valueOf()

b)

String.toInt()

c)

Integer.parseInt()

d)

Both 1 and 3

39.

What is the output of the following code snippet?
int a = 5;
int b = 10;
System.out.println(a + b);

a)

5

b)

10

c)

15

d)

50

40.

Which of the following is a valid way to declare an array in Java?

a)

int arr[];

b)

int[] arr;

c)

None of the above

d)

Both a and b

41.

What will be the output of the following code?
for(int i=0; i<5; i++) {
System.out.print(i + " ");
}

a)

0 1 2 3 4

b)

1 2 3 4 5

c)

0 1 2 3 4 5

d)

5 4 3 2 1

42.

Which keyword is used to create a subclass in Java?

a)

inherits

b)

implements

c)

subclass

d)

extends

43.

Which of the following is a valid way to declare an array in Java?

a)

Both A and B

b)

int arr[];

c)

int[] arr;

d)

None of the above

44.

What is the output of the following code: System.out.println(5 + 3 + "Hello");?

a)

Compilation Error

b)

8Hello

c)

53Hello

d)

Hello8

45.

Which keyword is used to prevent a method from being overridden?

a)

private

b)

static

c)

final

d)

protected

46.

What is the default value of a boolean variable in Java?

a)

true

b)

false

c)

0

d)

1

47.

Which of the following is NOT a valid access modifier in Java?

a)

public

b)

internal

c)

private

d)

protected

48.

What is the output of the following code snippet?
int a = 5;
int b = 10;
System.out.println(a + b);

a)

5 + 10

b)

10 + 5

c)

510

d)

15

49.

Which of the following is a valid way to declare an array in Java?

a)

int arr[];

b)

array int arr;

c)

int arr{};

d)

int[] arr;

50.

In the context of OOP, what does the term 'inheritance' refer to?

a)

Using multiple classes in a single program

b)

Defining a class without any methods

c)

Hiding the implementation details

d)

Creating a new class from an existing class