1. Write a Java program to print the numbers from 1 to 10 using a "for" loop.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
2. Write a Java program to find the sum of all even numbers between 1 and 100 using
a "for" loop.
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
System.out.println("Sum of even numbers between 1 and 100: " + sum);
}
}
3. Write a Java program to find the sum of all odd numbers between 1 and 100 using
a "for" loop.
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i += 2) {
sum += i;
}
System.out.println("Sum of odd numbers between 1 and 100: " + sum);
}
}
4. Create a Java program to print the even numbers from 2 to 10 using a "while"
loop.
public class Main {
public static void main(String[] args) {
int i = 2;
while (i <= 10) {
System.out.println(i);
i += 2;
}
}
}
5. Write a Java program to calculate the sum of numbers from 1 to 10 using a loop.
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum of numbers from 1 to 10: " + sum);
}
}
6. Create a Java program that prints the multiples of 3 from 3 to 30 using a loop.
public class Main {
public static void main(String[] args) {
for (int i = 3; i <= 30; i += 3) {
System.out.println(i);
}
}
}
7. Create a Java program that calculates the product of numbers from 1 to 5 using a
loop.
public class Main {
public static void main(String[] args) {
int product = 1;
for (int i = 1; i <= 5; i++) {
product *= i;
}
System.out.println("Product of numbers from 1 to 5: " + product);
}
}
8. Write a Java program to print the numbers from 10 to 1 in descending order using
a do-while loop.
public class Main {
public static void main(String[] args) {
int i = 10;
do {
System.out.println(i);
i--;
} while (i >= 1);
}
}
9. Create a Java program to find the largest among four numbers using else-if
ladder statements.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter four numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int largest = a;
if (b > largest) {
largest = b;
} else if (c > largest) {
largest = c;
} else if (d > largest) {
largest = d;
}
System.out.println("Largest number: " + largest);
}
}
10. Write a Java program that takes a number as input and prints whether it is
positive, negative, or zero.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num > 0) {
System.out.println("The number is positive.");
} else if (num < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}
11. Create a Java program that takes a character as input and checks if it is a
vowel using a switch conditional statement.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println("The character is a vowel.");
break;
default:
System.out.println("The character is not a vowel.");
}
}
}
12. Write a Java program that takes an age as input and determines if a person is a
child, teenager, adult, or senior.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
if (age < 12) {
System.out.println("Child");
} else if (age >= 13 && age <= 19) {
System.out.println("Teenager");
} else if (age >= 20 && age <= 59) {
System.out.println("Adult");
} else {
System.out.println("Senior");
}
}
}
13. What is JRE and JVM?
JVM (Java Virtual Machine): It is a part of the JRE that runs Java bytecode on any
device or operating system, providing platform independence.
JRE (Java Runtime Environment): It is a package that provides the necessary
libraries, Java Virtual Machine (JVM), and other components required to run Java
applications.
14. Difference between for loop, while loop, and do-while loop:
For loop: The initialization, condition check, and update happen in a single line.
Used when the number of iterations is known beforehand.
While loop: The condition is checked before executing the loop. It might not
execute if the condition is false initially.
Do-while loop: Similar to the while loop but guarantees at least one execution of
the loop body before checking the condition.
15. Difference between print and println:
print: Prints the output without moving to the next line.
println: Prints the output and moves the cursor to the next line.
16. Difference between formal and actual arguments:
Formal argument: These are parameters defined in a method declaration.
Actual argument: These are the values passed to the method when calling it.
Rules:
Number of actual arguments must match the number of formal arguments.
Type and order must also match.
17. Difference between return type and return statement:
Return type: The data type of the value a method is expected to return.
Return statement: The statement that returns a value from the method to the caller.
18. What is a method? Write syntax of a method and describe each term:
Method: A block of code that performs a specific task.
Syntax:
returnType methodName(parameterList) {
// method body
}
ReturnType: Specifies the type of value returned by the method.
MethodName: The name of the method.
ParameterList: A list of parameters (optional).
Method Body: The code that defines the functionality of the method.
19. Write a Java program to print the sum, average, sum of even numbers, sum of odd
numbers of an array.
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0, evenSum = 0, oddSum = 0;
for (int num : arr) {
sum += num;
if (num % 2 == 0) {
evenSum += num;
} else {
oddSum += num;
}
}
double average = sum / (double) arr.length;
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
System.out.println("Sum of even numbers: " + evenSum);
System.out.println("Sum of odd numbers: " + oddSum);
}
}