WorksheetsJava - I/O,Condition
Total questions: 45
Worksheet time: 33mins
What is the correct syntax to declare a variable in Java?
dataType variableName; (e.g., int num;)
var variableName = dataType;
dataType: variableName;
variableName dataType;
Which of the following is a valid data type in Java?
Integer
Character
String
Boolean
How do you read a file in Java using FileReader?
BufferedReader bufferedReader = new BufferedReader();
FileReader fileReader = new FileReader("file.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } bufferedReader.close();
String line = bufferedReader.readLine();
FileReader fileReader = new FileReader();
What method is used to write data to a file in Java?
DataOutputStream
FileReader
FileWriter or BufferedWriter
PrintWriter
What is the purpose of the 'if' statement in Java?
The purpose of the 'if' statement in Java is to execute code conditionally based on a boolean expression.
To define a variable in Java.
To create a loop in Java.
To import libraries in Java.
How do you handle exceptions in Java?
Use try-catch blocks to handle exceptions in Java.
Ignore exceptions and continue execution.
Throw exceptions without handling them.
Use if-else statements to handle exceptions in Java.
What is the difference between '==' and '.equals()' in Java?
The '==' operator is used for primitive types, while '.equals()' is for reference types.
The '==' operator checks if two references point to the same object, while '.equals()' checks if two objects are logically equal.
'==' compares object types, while '.equals()' compares memory addresses.
Both '==' and '.equals()' perform the same function in Java.
Which class is used to create a file in Java?
File
FileWriter
Document
OutputStream
What is the output of the following code: System.out.println(5 > 3 ? 'A' : 'B');?
C
5
B
A
How do you check if a file exists in Java?
File file = new File("path/to/file"); boolean exists = file.exists();
File file = new File(); boolean exists = file.isFile();
File file = new File("path/to/file"); boolean exists = file.isDirectory();
boolean exists = Files.exists(Paths.get("path/to/file"));
What is the role of the 'else' statement in a conditional structure?
The 'else' statement executes code when the 'if' condition is false.
The 'else' statement is used to define multiple 'if' conditions.
The 'else' statement always executes regardless of the 'if' condition.
The 'else' statement is used to terminate a conditional structure.
How can you read a line from a file using BufferedReader in Java?
BufferedReader br = new BufferedReader(new FileReader()); String line = br.read(); br.close();
BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine(); br.flush();
BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine(); br.close();
BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine(); br.open();
What is the purpose of the 'switch' statement in Java?
To execute a block of code based on the value of a variable.
To create a loop that iterates over a collection.
To define a method in Java.
To handle exceptions in Java.
Which of the following keywords is used to define a constant in Java?
constant
static
const
final
What is the output of the following code: System.out.println(10 % 3);?
3
10
1
0
Java is a
Procedure Oriented Language
Structure Oriented Language
Object_Oriented Language
Machine Language
The name of the following should be the file name in Java
Object Name
Variable Nam
Class Name
Array Name
Java program is
Interpreted
Compiled
Java program does not run without below function
user()
system()
main()
void()
A ___________ file is created after successful compilation of a Java program.
Object file
Array file
Class file
String file
Command to compile Java program
javax
javay
javac
Java
Command to run a Java program
javaa
java
jaava
jaavaa
JDK stands for
Java Developer Kit
Java Development Kit
Java Design Kit
Java Debugging Kit
JRE stands for
Java Run Environment
Java Run Execution
Java Runtime Environment
Java Ready Execution
Java is
Machine dependent language
Machine independent language
Which is not included in Java Conditional statements?
if
else
else if
switch
none
Which conditional statement is used to specify a block of code to be executed if a specified condition is false?
if
else
else if
switch
What is the syntax to check if a variable 'x' is greater than 10 in Java?
if x > 10
if x >= 10
if x < 10
if x <= 10
Which of the following is not a comparison operator in Java?
==
<=
&&
!=
When should you use the else if statement in Java?
To specify a block of code to be executed if a specified condition is true.
To specify many alternative blocks of code to be executed.
To specify a new condition to test, if the first condition is false.
To specify a block of code to be executed if the same condition is false.
What is the correct syntax for a switch statement in Java?
switch { }
switch (expression) { }
switch expression { }
switch (expression) { case: }
Which conditional statement would you use to specify many alternative blocks of code to be executed?
if
else
else if
switch
What does the following code snippet do in Java?
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."
It checks if the age is less than 18 and prints "You are an adult." if true, else "You are a minor."
It checks if the age is exactly 18 and prints "You are an adult." if true, else "You are a minor."
It always prints "You are an adult."
What will be the output of the following code snippet in Java?
x is not 5.
x is 5.
Compilation Error
Runtime Error
What is the result of the following expression in Java?
Condition is true
Condition is false
Compilation Error
Runtime Error
What is the syntax for an if-else statement in Java?
How do you write a nested if statement in Java?
Use a while loop within the body of an if statement.
Use a switch statement within the body of an if statement.
Use a for loop within the body of an if statement.
Include an if statement within the body of another if statement.
What is the purpose of a switch statement in Java?
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.
The purpose of a switch statement in Java is to handle exceptions.
The purpose of a switch statement in Java is to define a new variable.
The purpose of a switch statement in Java is to loop through a list of values.
What is the syntax for a ternary operator in Java?
condition ? expression1 : expression2
condition ? expression1 : expression2
condition ? expression2 : expression1
condition ? expression1 : expression3
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"); }
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"); }
How do you write a nested if-else statement in Java?
Use a switch statement instead of nested if-else statements.
Use a for loop instead of nested if-else statements.
Include another if-else statement within the body of an existing if or else block.
Use a while loop instead of nested if-else statements.
What is the purpose of a break statement in a switch statement?
To continue to the next case in the switch statement
To restart the switch statement from the beginning
To skip the current case and execute the default case
To exit the switch statement and prevent further code execution
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"); }
What is the syntax for a nested ternary operator in Java?
result = condition1 ? value1 : (condition2 ? value2 : value4);
result = condition1 ? value1 : (condition2 ? value4 : value3);
result = condition1 ? value1 : (condition4 ? value2 : value3);
result = condition1 ? value1 : (condition2 ? value2 : value3);
