Back to notes
Briefly describe the purpose of the `default` case in a `switch` statement.
Press to flip
The `default` case in a `switch` statement is executed if none of the specified cases match the switch expression.
Write a basic example of an `if` statement in Java.
if(fruit.equals('mango')) { print('King of fruits'); }
Give an example of using the Scanner class to read a user's input.
Scanner scanner = new Scanner(System.in); String fruit = scanner.nextLine(); // Code to process the input
Explain how object references and memory allocation work in Java.
In Java, objects are stored in memory, and multiple references can point to the same object, allowing changes through any reference to reflect on the same object.
What is a practical use of the Scanner class in Java?
The Scanner class is used to take different types of user input, which can then be processed accordingly in the program.
Provide a code example demonstrating a `switch` statement handling multiple fruit types.
switch(fruit) { case 'mango': print('King of fruits'); break; case 'orange': print('Citrus fruit'); break; default: print('Unknown fruit'); }
How does an `if` statement work?
An `if` statement executes a block of code if the specified condition is true.
What is the function of an `else` statement in conditionals?
An `else` statement provides an alternative block of code to execute if the `if` condition is false.
Describe a `switch` statement and its purpose.
A `switch` statement provides a more readable and organized way to handle multiple conditional checks based on the value of a variable.
In the context of conditional statements, what is meant by 'scenario handling'?
Scenario handling refers to the process of writing code to perform different actions based on evaluated conditions, like handling various fruit types in the examples provided.
What is the purpose of conditional statements in programming?
Conditional statements are used to perform different actions based on different conditions.
Write a code example to compare two strings `str1` and `str2` in Java.
if (str1.equals(str2)) { /* do something */ }
What does the `break` statement do inside a switch case?
The `break` statement exits the switch block, preventing the execution of subsequent cases.
What are some common string manipulation methods in Java?
`equals`, `substring`, `charAt`, among others.
Why is it important to compare strings using the `equals` method instead of `==` in Java?
Using `equals` compares the actual content of the strings, while `==` compares the memory addresses, which can lead to unexpected results.
Previous
Next