and Programming Chapter 2
(Programming Projects) Questions and
Answers 100% Pass
What is the role of a variable in problem-solving and programming?
✔✔ A variable is used to store data that can be manipulated during the execution of a program.
How do you declare and initialize an integer variable named `score` with the value 100?
✔✔ `int score = 100;`
How can you write a program that calculates the sum of two numbers in Java?
✔✔ `int num1 = 5; int num2 = 7; int sum = num1 + num2;`
What is the purpose of using `Scanner` in Java for problem-solving?
✔✔ `Scanner` is used to take input from the user during the execution of the program.
How do you calculate the area of a circle in Java?
✔✔ `double radius = 5; double area = Math.PI * radius * radius;`
1
,How do you write a Java program to calculate the perimeter of a rectangle?
✔✔ `int length = 5; int width = 10; int perimeter = 2 * (length + width);`
How do you swap the values of two variables in Java?
✔✔ `int temp = a; a = b; b = temp;`
How do you determine if a number is even or odd in Java?
✔✔ `if (num % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }`
What is the basic syntax for creating a method in Java?
✔✔ `public returnType methodName(parameters) { // method body }`
How do you write a Java program that checks whether a number is positive, negative, or zero?
✔✔ `if (num > 0) { System.out.println("Positive"); } else if (num < 0) {
System.out.println("Negative"); } else { System.out.println("Zero"); }`
What is a loop and how can it be used in problem-solving in Java?
2
,✔✔ A loop allows repeated execution of a block of code, useful for tasks that require repetition
like counting or processing items in a list.
How do you calculate the factorial of a number in Java?
✔✔ `int factorial = 1; for (int i = 1; i <= num; i++) { factorial *= i; }`
How can you calculate the power of a number in Java?
✔✔ `double result = Math.pow(base, exponent);`
What is the purpose of the `break` statement in Java?
✔✔ The `break` statement exits from the current loop or switch statement immediately.
How do you write a program that prints the Fibonacci sequence up to a given number?
✔✔ `int a = 0, b = 1, c; for (int i = 0; i < n; i++) { c = a + b; a = b; b = c; System.out.println(a);
}`
How do you check if a number is a prime number in Java?
3
, ✔✔ `boolean isPrime = true; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { isPrime =
false; break; } } if (isPrime) { System.out.println("Prime"); } else { System.out.println("Not
prime"); }`
What is the basic structure of a conditional statement in Java?
✔✔ `if (condition) { // code to execute if condition is true } else { // code to execute if condition
is false }`
How do you calculate the average of three numbers in Java?
✔✔ `double average = (num1 + num2 + num3) / 3.0;`
What is the difference between `++` and `--` operators in Java?
✔✔ `++` increments a variable by 1, while `--` decrements a variable by 1.
How can you find the largest of three numbers in Java?
✔✔ `int largest = Math.max(num1, Math.max(num2, num3));`
How do you implement a simple calculator program in Java?
4