Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java - I/O,Condition

Total questions: 45

Worksheet time: 33mins

Name
Class
Date
1.

What is the correct syntax to declare a variable in Java?

a)

dataType variableName; (e.g., int num;)

b)

var variableName = dataType;

c)

dataType: variableName;

d)

variableName dataType;

2.

Which of the following is a valid data type in Java?

a)

Integer

b)

Character

c)

String

d)

Boolean

3.

How do you read a file in Java using FileReader?

a)

BufferedReader bufferedReader = new BufferedReader();

b)

FileReader fileReader = new FileReader("file.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } bufferedReader.close();

c)

String line = bufferedReader.readLine();

d)

FileReader fileReader = new FileReader();

4.

What method is used to write data to a file in Java?

a)

DataOutputStream

b)

FileReader

c)

FileWriter or BufferedWriter

d)

PrintWriter

5.

What is the purpose of the 'if' statement in Java?

a)

The purpose of the 'if' statement in Java is to execute code conditionally based on a boolean expression.

b)

To define a variable in Java.

c)

To create a loop in Java.

d)

To import libraries in Java.

6.

How do you handle exceptions in Java?

a)

Use try-catch blocks to handle exceptions in Java.

b)

Ignore exceptions and continue execution.

c)

Throw exceptions without handling them.

d)

Use if-else statements to handle exceptions in Java.

7.

What is the difference between '==' and '.equals()' in Java?

a)

The '==' operator is used for primitive types, while '.equals()' is for reference types.

b)

The '==' operator checks if two references point to the same object, while '.equals()' checks if two objects are logically equal.

c)

'==' compares object types, while '.equals()' compares memory addresses.

d)

Both '==' and '.equals()' perform the same function in Java.

8.

Which class is used to create a file in Java?

a)

File

b)

FileWriter

c)

Document

d)

OutputStream

9.

What is the output of the following code: System.out.println(5 > 3 ? 'A' : 'B');?

a)

C

b)

5

c)

B

d)

A

10.

How do you check if a file exists in Java?

a)

File file = new File("path/to/file"); boolean exists = file.exists();

b)

File file = new File(); boolean exists = file.isFile();

c)

File file = new File("path/to/file"); boolean exists = file.isDirectory();

d)

boolean exists = Files.exists(Paths.get("path/to/file"));

11.

What is the role of the 'else' statement in a conditional structure?

a)

The 'else' statement executes code when the 'if' condition is false.

b)

The 'else' statement is used to define multiple 'if' conditions.

c)

The 'else' statement always executes regardless of the 'if' condition.

d)

The 'else' statement is used to terminate a conditional structure.

12.

How can you read a line from a file using BufferedReader in Java?

a)

BufferedReader br = new BufferedReader(new FileReader()); String line = br.read(); br.close();

b)

BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine(); br.flush();

c)

BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine(); br.close();

d)

BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine(); br.open();

13.

What is the purpose of the 'switch' statement in Java?

a)

To execute a block of code based on the value of a variable.

b)

To create a loop that iterates over a collection.

c)

To define a method in Java.

d)

To handle exceptions in Java.

14.

Which of the following keywords is used to define a constant in Java?

a)

constant

b)

static

c)

const

d)

final

15.

What is the output of the following code: System.out.println(10 % 3);?

a)

3

b)

10

c)

1

d)

0

16.

Java is a

a)

Procedure Oriented Language

b)

Structure Oriented Language

c)

Object_Oriented Language

d)

Machine Language

17.

The name of the following should be the file name in Java

a)

Object Name

b)

Variable Nam

c)

Class Name

d)

Array Name

18.

Java program is

a)

Interpreted

b)

Compiled

19.

Java program does not run without below function

a)

user()

b)

system()

c)

main()

d)

void()

20.

A ___________ file is created after successful compilation of a Java program.

a)

Object file

b)

Array file

c)

Class file

d)

String file

21.

Command to compile Java program

a)

javax

b)

javay

c)

javac

d)

Java

22.

Command to run a Java program

a)

javaa

b)

java

c)

jaava

d)

jaavaa

23.

JDK stands for

a)

Java Developer Kit

b)

Java Development Kit

c)

Java Design Kit

d)

Java Debugging Kit

24.

JRE stands for

a)

Java Run Environment

b)

Java Run Execution

c)

Java Runtime Environment

d)

Java Ready Execution

25.

Java is

a)

Machine dependent language

b)

Machine independent language

26.

Which is not included in Java Conditional statements?

a)

if

b)

else

c)

else if

d)

switch

e)

none

27.

Which conditional statement is used to specify a block of code to be executed if a specified condition is false?

a)

if

b)

else

c)

else if

d)

switch

28.

What is the syntax to check if a variable 'x' is greater than 10 in Java?

a)

if x > 10

b)

if x >= 10

c)

if x < 10

d)

if x <= 10

29.

Which of the following is not a comparison operator in Java?

a)

==

b)

<=

c)

&&

d)

!=

30.

When should you use the else if statement in Java?

a)

To specify a block of code to be executed if a specified condition is true.

b)

To specify many alternative blocks of code to be executed.

c)

To specify a new condition to test, if the first condition is false.

d)

To specify a block of code to be executed if the same condition is false.

31.

What is the correct syntax for a switch statement in Java?

a)

switch { }

b)

switch (expression) { }

c)

switch expression { }

d)

switch (expression) { case: }

32.

Which conditional statement would you use to specify many alternative blocks of code to be executed?

a)

if

b)

else

c)

else if

d)

switch

33.

What does the following code snippet do in Java?

a)

It checks if the age is greater than or equal to 18 and prints "You are an adult." if true, else "You are a minor."

b)

It checks if the age is less than 18 and prints "You are an adult." if true, else "You are a minor."

c)

It checks if the age is exactly 18 and prints "You are an adult." if true, else "You are a minor."

d)

It always prints "You are an adult."

34.

What will be the output of the following code snippet in Java?

a)

x is not 5.

b)

x is 5.

c)

Compilation Error

d)

Runtime Error

35.

What is the result of the following expression in Java?

a)

Condition is true

b)

Condition is false

c)

Compilation Error

d)

Runtime Error

36.

What is the syntax for an if-else statement in Java?

4 lines
37.

How do you write a nested if statement in Java?

a)

Use a while loop within the body of an if statement.

b)

Use a switch statement within the body of an if statement.

c)

Use a for loop within the body of an if statement.

d)

Include an if statement within the body of another if statement.

38.

What is the purpose of a switch statement in Java?

a)

The purpose of a switch statement in Java is to provide a way to execute different code blocks based on the value of a variable or expression.

b)

The purpose of a switch statement in Java is to handle exceptions.

c)

The purpose of a switch statement in Java is to define a new variable.

d)

The purpose of a switch statement in Java is to loop through a list of values.

39.

What is the syntax for a ternary operator in Java?

a)

condition ? expression1 : expression2

b)

condition ? expression1 : expression2

c)

condition ? expression2 : expression1

d)

condition ? expression1 : expression3

40.

What is the result of the following code snippet? int x = 5; int y = 10; if (x > y) { System.out.println("x is greater than y"); } else { System.out.println("x is less than or equal to y"); }

4 lines
41.

What is the output of the following code snippet? int num = 2; if (num % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }

4 lines
42.

How do you write a nested if-else statement in Java?

a)

Use a switch statement instead of nested if-else statements.

b)

Use a for loop instead of nested if-else statements.

c)

Include another if-else statement within the body of an existing if or else block.

d)

Use a while loop instead of nested if-else statements.

43.

What is the purpose of a break statement in a switch statement?

a)

To continue to the next case in the switch statement

b)

To restart the switch statement from the beginning

c)

To skip the current case and execute the default case

d)

To exit the switch statement and prevent further code execution

44.

What is the result of the following code snippet? int x = 10; switch (x) { case 5: System.out.println("Five"); break; case 10: System.out.println("Ten"); break; default: System.out.println("Other"); }

4 lines
45.

What is the syntax for a nested ternary operator in Java?

a)

result = condition1 ? value1 : (condition2 ? value2 : value4);

b)

result = condition1 ? value1 : (condition2 ? value4 : value3);

c)

result = condition1 ? value1 : (condition4 ? value2 : value3);

d)

result = condition1 ? value1 : (condition2 ? value2 : value3);