JAVA QUESTIONS FOR VIVA
1. What is Java?
Java is a programming language used to make computer programs.
2. Who invented Java and in which year?
Java was invented by James Gosling in 1995.
3. Why is Java called platform-independent?
Because Java programs can run on any computer system without changes.
4. What is JVM?
JVM stands for Java Virtual Machine. It runs Java programs.
5. What is BlueJ?
BlueJ is software used to write and run Java programs in schools.
6. What is a program?
A program is a set of instructions given to a computer.
7. What is a class?
A class is a model or blueprint from which objects are created.
8. What is an object?
An object is something created from a class and has properties and actions.
9. Example of real-life objects?
Car, fan, and mobile phone.
10. What is a method?
A method is a block of code that performs a task.
11. Difference between main() and other methods?
main() is the starting point of the program; other methods do additional tasks.
12. What are comments in Java?
Comments are notes for programmers that the computer ignores.
13. Types of comments?
Single-line (//) and multi-line (/* */).
14. Is Java case-sensitive?
Yes, Java treats uppercase and lowercase separately.
15. Rules for naming variables?
Should not start with a number, no spaces, and not a Java keyword.
16. What is a variable?
A variable is memory used to store a value.
17. What is a constant?
A constant is a value that cannot be changed.
18. Keyword used for constant?
final
19. What are data types?
Data types tell what type of value a variable can store.
20. Primitive data types?
int, char, double, boolean etc.
21. Difference between int, char, and double?
int = whole numbers, char = one character, double = decimal numbers.
22. What is a string?
A string is a group of characters like a word or sentence.
23. What is type casting?
Converting one data type into another.
24. What are operators?
Operators perform calculations and comparisons.
25. Assignment operator?
= assigns value to a variable.
26. Difference between = and == ?
= assigns value, == checks if values are equal.
27. Arithmetic operators?
+, -, *, /, %
28. Relational operators?
29. , <, >=, <=, ==, !=
30. Logical operators?
&& (and), || (or), ! (not)
31. What are ++ and -- ?
++ increases value by 1, -- decreases value by 1.
32. What is Scanner class?
Scanner class is used to take input from the user.
33. Which package contains Scanner?
[Link] package.
34. Statement to create Scanner object?
Scanner sc = new Scanner([Link]);
35. Method to take integer input?
nextInt()
36. Method to take string input?
nextLine()
37. Use of [Link]()?
It prints output on the screen and moves to the next line.
38. What is a conditional statement?
A statement that checks conditions and makes decisions.
39. Why use if-else?
To run one part of code when condition is true and another when false.
40. What is nested if?
An if statement inside another if statement.
41. What is switch?
A statement used to choose one option from many options.
42. Difference between if-else and switch?
if-else works on conditions; switch works on fixed values.
43. What is a loop?
A loop repeats a block of code again and again.
44. Types of loops?
for loop, while loop, do-while loop.
45. What is a for loop?
A loop used when number of repetitions is known.
46. Difference between while and do-while?
while checks condition first; do-while runs once before checking.
47. Which loop runs at least once?
do-while loop.
48. Parts of BlueJ window?
Class diagram area, object bench, terminal window, editor window.
49. What is the editor window?
The place where we write and edit the program.
50. Use of Compile button?
To check and remove errors.
51. What happens if the program has an error?
BlueJ shows an error message and does not run the program until corrected.
52. How to call a method in BlueJ?
Create an object → click on the method name.
SWITCH CASE
Q1. What is a switch statement?
A switch statement is a type of control statement used in Java to make decisions. It allows the computer to select one
specific block of code to run from many available options, depending on the value of a variable or expression. It is
very useful when we have many fixed choices, such as days of the week, months, menu choices, etc.
Q2. Why do we use a switch statement?
We use a switch statement when we want to check one value against several possible fixed values. Instead of writing
many if-else statements, switch helps to make the code short, clean, and easier to read. It also improves clarity when
there are many options to choose from.
Q3. What is a case in switch?
A case represents one specific choice inside the switch. Each case has a value, and when the switch expression
matches that value, the code inside that case runs. Every case value must be unique so that the program knows
exactly which code to execute.
Q4. What is the purpose of the break statement in switch?
The break statement is used to stop the execution inside the switch after a case has matched and its code has run.
Without break, the program continues to execute the next cases also, even if they do not match. This is called “fall-
through,” so using break helps to run only the correct case.
Q5. What is the default case?
The default case is a part of the switch that runs when none of the other cases match the value. It works like a safety
net. If the user enters a wrong or unexpected value, default will run and help avoid mistakes or blank output. It is not
compulsory but recommended.
Q6. What happens if break is not used?
If break is not used, then after one case is matched, the program will continue to execute the next cases also, even if
they do not match the value. This is called fall-through. Sometimes this is useful, but usually it causes wrong outputs,
so we should write break in most cases.
Q7. Why is switch sometimes better than if-else?
Switch is sometimes better than if-else because when we have many fixed choices, switch looks more organized and
easier to understand. It avoids writing a long chain of if-else statements. It makes the program shorter and improves
clarity.