Java Short Questions with Detailed Answers
1. What is Java Basic Syntax?
Java basic syntax refers to the rules used to write Java programs correctly.
Every Java program starts with a class, and execution begins from the main() method. Java
is case-sensitive and every statement ends with a semicolon (;).
Example:
class Test {
public static void main(String[] args) {
[Link]("Hello");
}
}
2. What is a Simple Expression Program in Java?
A simple expression program performs calculations using operators such as +, -, *, /, and %.
Example:
int a = 10;
int b = 5;
int sum = a + b;
[Link](sum);
Output:
15
3. What is the if Statement in Java?
The if statement is used to check a condition. If the condition is true, the block executes.
Example:
int age = 18;
if(age >= 18) {
[Link]("Eligible");
}
4. What is an if-else-if Ladder?
It is used to test multiple conditions one after another.
Example:
int marks = 75;
if(marks >= 80) {
[Link]("A");
}
else if(marks >= 60) {
[Link]("B");
}
else {
[Link]("Fail");
}
5. What is a Switch Statement?
The switch statement selects one option from many cases.
Example:
int day = 2;
switch(day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
default:
[Link]("Invalid");
}
6. What is a Loop in Java?
A loop repeats statements until a condition becomes false.
Types:
- for loop
- while loop
- do-while loop
Example:
for(int i=1; i<=5; i++) {
[Link](i);
}
7. What is a for Loop?
A for loop is used when the number of repetitions is known.
Syntax:
for(initialization; condition; increment/decrement)
Example:
for(int i=1; i<=3; i++) {
[Link](i);
}
8. What is a while Loop?
The while loop executes while the condition remains true.
Example:
int i = 1;
while(i <= 3) {
[Link](i);
i++;
}
9. What is a Method in Java?
A method is a block of code that performs a specific task and can be reused.
Example:
class Test {
static void greet() {
[Link]("Welcome");
}
public static void main(String[] args) {
greet();
}
}
10. What is Method Definition?
Method definition means writing the complete method body.
Syntax:
returnType methodName(parameters) {
// code
}
Example:
int add(int a, int b) {
return a + b;
}