NEW
Font size
WorksheetsCS0007 - Diagnostic Exam
Total questions: 50
Worksheet time: 25mins
Which tool is used to interpret a Java program?
javac
JDK
JVM
JRE
What does the Java compiler do?
Compiles source code into byte code
Compiles source code into machine code
Compiles byte code into machine code
Compiles byte code into source code
What does the Java interpreter do?
Interprets source code into byte code
Interprets source code into machine code
Interprets byte code into machine code
Interprets byte code into source code
Given the program: public class Test { } What should be the complete file name when saving the given Java program?
Test.class
Test.jar
Test.txt
Test.java
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"); }
Normal Year
Leap Year
Compilation Error
Runtime Error
With the statement, int x = -((32<<2) ^ (520>>2)); What is the value of x?
-2089
2088
2
-3
Which statement is NOT equal to ++x?
x++
x=1+x
x=+1
x+=1
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. 9
b. 10
c. No output due to syntax error.
d. No output due to infinite loop.
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"; } } } }
i % 2 == 0
i / 2 == 0
i % 2 == 1
i / 2 == 0
i == 2
With the program: class Test { public void message() {} int message(String msg) { return msg; } } What OOP concept is shown in the given program?
encapsulation
inheritance
polymorphism
abstraction
Which variable is accessible within the class A?
w and z
w, z and x
all except Y
All of the variables
Which is an instance variable?
z
Y
w and x
w, x and Y
Which is a class variable?
z
Y
w and x
w, x and Y
Which method is automatically executed when creating an object?
main method
standard method
custom method
constructor method
Which statement will display the speed attribute of the class Car in method main()?
mazda.speed
this.speed
super.speed
bmw.speed
Which statement will display the speed attribute of the class SportsCar in method main()?
this.speed
bmw.speed
mazda.speed
super.speed
Which statement is true regarding speed variable?
The variable speed has been overridden in class SportsCar.
The variable speed of Car can’t be inherited by SportsCar due to encapsulation.
The variable speed has been hidden from class SportsCar since Java doesn’t support overloading of variables.
The variable speed has been overloaded in class SportsCar.
Which variable will require a standard method in Car?
speed
price
model
brand
Which is true about the SportsCar? (Choose the best answer)
The SportsCar will inherit all the non-private attributes and operations of its parent class.
The SportsCar will inherit all the non-private attributes and operations of its based class and its ancestors.
SportsCar will inherit all the class attributes and operations of its super class.
SportsCar will inherit all the attributes and operations of its parent class and its ancestors.
It is used to forcibly or manually throw an exception.
try
catch
throw
throws
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)); } }
3.0
2.0
4.0
1.0
In using the class FileInputStream, what is the return value of read() method when the end of file has been reached?
null
Boolean value
-1
0
Which of the following correctly initializes a FileInputStream object for reading the file "test.txt"?
FileOutputStream fis = new FileOutputStream("test.txt");
FileOutputStream fis = new FileOutputStream(test.txt);
FileInputStream fis = new FileInputStream("test.txt");
FileInputStream fis = new FileInputStream(test.txt);
What condition should be used in the while loop to ensure the file is read until the end?
fis.read() != -1
(value=fis.read()) != -1
(value=fis.read()) != null
fis.read() != null
Which is a standard output stream in Java?
FileOutputStream
System.out
FileWriter
DataOutputStream
HashLinkedSet class represents the LinkedList implementation of Set Interface.
True
False
SortedSet is the alternate of Set interface that provides a total ordering on its elements.
True
False
It's called a stream because it is like a stream of water that continues to flow.
True
False
OutputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes.
True
False
The iterable interface is the root interface for all the collection classes.
True
False
Which method is used to read a single byte from a FileInputStream?
fetchByte()
read()
readByte()
getByte()
What is the default access modifier for class members in Java?
public
package-private
protected
private
Which of the following is a valid way to create an instance of a String in Java?
String str = 'Hello';
String str = new String();
String str = String();
String str = "Hello";
What does the keyword 'static' signify in a method declaration?
The method can be overridden
The method belongs to the class rather than instances
The method is private
The method is synchronized
What is the default value of a boolean variable in Java?
true
false
1
0
Which of the following is not a valid Java identifier?
myVariable
$myVariable
_myVariable
2ndVariable
What keyword is used to create a subclass in Java?
extends
implements
super
inherits
Which of the following methods can be used to convert a string to an integer in Java?
Integer.valueOf()
String.toInt()
Integer.parseInt()
Both 1 and 3
What is the output of the following code snippet?
int a = 5;
int b = 10;
System.out.println(a + b);
5
10
15
50
Which of the following is a valid way to declare an array in Java?
int arr[];
int[] arr;
None of the above
Both a and b
What will be the output of the following code?
for(int i=0; i<5; i++) {
System.out.print(i + " ");
}
0 1 2 3 4
1 2 3 4 5
0 1 2 3 4 5
5 4 3 2 1
Which keyword is used to create a subclass in Java?
inherits
implements
subclass
extends
Which of the following is a valid way to declare an array in Java?
Both A and B
int arr[];
int[] arr;
None of the above
What is the output of the following code: System.out.println(5 + 3 + "Hello");?
Compilation Error
8Hello
53Hello
Hello8
Which keyword is used to prevent a method from being overridden?
private
static
final
protected
What is the default value of a boolean variable in Java?
true
false
0
1
Which of the following is NOT a valid access modifier in Java?
public
internal
private
protected
What is the output of the following code snippet?
int a = 5;
int b = 10;
System.out.println(a + b);
5 + 10
10 + 5
510
15
Which of the following is a valid way to declare an array in Java?
int arr[];
array int arr;
int arr{};
int[] arr;
In the context of OOP, what does the term 'inheritance' refer to?
Using multiple classes in a single program
Defining a class without any methods
Hiding the implementation details
Creating a new class from an existing class
