Here are 50 basic Java questions focusing on single loops and related concepts:
Basic Loop Syntax and Logic
1. What is the syntax of a for loop in Java?
2. How does a while loop work in Java?
3. How does a do-while loop differ from a while loop?
4. Write a for loop to print numbers from 1 to 10.
5. Write a while loop to print numbers from 10 to 1.
6. Write a do-while loop to print "Hello World" 5 times.
7. What is the difference between break and continue in loops?
8. How can you exit a loop prematurely?
9. How can you skip the current iteration in a loop?
10. What happens if the condition in a while loop is always true?
Practical Applications of Loops
11. Write a for loop to calculate the sum of numbers from 1 to 100.
12. Write a while loop to find the factorial of a number.
13. Write a program using a loop to check if a number is prime.
14. Write a loop to reverse a given number.
15. Write a loop to calculate the sum of digits of a number.
16. Write a loop to find the largest digit in a number.
17. Write a loop to print all even numbers from 1 to 50.
18. Write a loop to print all odd numbers from 1 to 50.
19. Write a loop to count the number of digits in a number.
20. Write a program to print the multiplication table of a number using a loop.
Nested Loops (Light Introduction)
21. Can you use a single loop to print a triangle pattern? Why or why not?
22. How would you print a simple square of * using a single loop?
23. Write a program to print the first 10 Fibonacci numbers using a single loop.
24. Write a loop to check if a number is an Armstrong number.
25. How can you find the GCD of two numbers using a loop?
26. Write a loop to check if a string is a palindrome.
27. Write a loop to count the occurrences of a character in a string.
28. Write a loop to calculate the power of a number (e.g., aba^bab).
29. Write a loop to convert a binary number to its decimal equivalent.
30. Write a loop to print all numbers divisible by 3 and 5 between 1 and 100.
Loop and Arrays
31. Write a loop to find the maximum element in an array.
32. Write a loop to find the minimum element in an array.
33. Write a loop to calculate the sum of elements in an array.
34. Write a loop to reverse an array.
35. Write a loop to count positive and negative numbers in an array.
36. Write a loop to check if an array is sorted in ascending order.
37. Write a loop to print all elements at even indices in an array.
38. Write a loop to find duplicate elements in an array.
39. Write a loop to print all unique elements in an array.
40. Write a loop to rotate an array to the left by one position.
Miscellaneous Questions
41. What happens if the condition in a for loop is omitted?
42. Can a for loop run indefinitely? Provide an example.
43. What is an infinite loop? How can you create one?
44. How can you use a loop to simulate a countdown timer?
45. Write a loop to calculate the sum of all odd-positioned digits in a number.
46. Write a loop to generate the first n terms of an arithmetic progression.
47. Write a loop to find the sum of all prime numbers below 100.
48. Write a program to find the smallest number in an array greater than a given number
using a loop.
49. Write a program to print all palindrome numbers between 1 and 100 using a loop.
50. Write a loop to check if two strings are anagrams of each other.
Here are 50 multiple-choice questions (MCQs) on Basic Java Features including Single
Loops:
Java Basics
1. Which of the following is not a Java feature?
a) Object-Oriented
b) Platform-Independent
c) Low-Level Programming Language
d) Robust
Answer: c
2. Which of these is the correct way to declare a variable in Java?
a) int 1x = 10;
b) int x1 = 10;
c) int x = 10.5;
d) int x; x = "10";
Answer: b
3. What is the size of an int in Java?
a) 16 bits
b) 32 bits
c) 64 bits
d) 8 bits
Answer: b
4. Which of the following is used to compile Java code?
a) javac
b) java
c) jre
d) jvm
Answer: a
5. Which of the following is not a valid Java data type?
a) byte
b) float
c) real
d) char
Answer: c
Control Statements and Loops
6. What is the output of the following code?
java
Copy code
for (int i = 0; i < 5; i++) {
[Link](i + " ");
}
a) 1 2 3 4 5
b) 0 1 2 3 4
c) 0 1 2 3 4 5
d) 1 2 3 4
Answer: b
7. Which loop executes at least once, regardless of the condition?
a) for
b) while
c) do-while
d) None of the above
Answer: c
8. Which keyword is used to exit a loop prematurely?
a) continue
b) break
c) return
d) exit
Answer: b
9. What is the output of the following code?
java
Copy code
int i = 0;
while (i < 3) {
[Link](i + " ");
i++;
}
a) 1 2 3
b) 0 1 2
c) 0 1 2 3
d) Infinite loop
Answer: b
10. Which of the following is true about for loops?
a) The loop variable must be an integer.
b) The loop condition is checked after each iteration.
c) The loop body executes before the condition is checked.
d) None of the above.
Answer: b
Single Loops
11. What will be the output of the following code?
java
Copy code
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
[Link](i + " ");
}
a) 1 2 3 4 5
b) 1 2
c) 1 2 3
d) None of the above
Answer: b
12. What will the following code print?
java
Copy code
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
[Link](i + " ");
}
a) 0 1 2 3 4
b) 1 3
c) 0 2 4
d) None of the above
Answer: b
13. Which of the following can be used as a loop control variable?
a) int
b) char
c) float
d) All of the above
Answer: d
14. What is the output of the following code?
java
Copy code
int i = 5;
while (i > 0) {
[Link](i + " ");
i--;
}
a) 5 4 3 2 1
b) 4 3 2 1 0
c) 5 4 3 2
d) None of the above
Answer: a
15. What will happen if the condition in a while loop is always true?
a) The loop will execute once.
b) The loop will execute twice.
c) The loop will run indefinitely.
d) Compilation error.
Answer: c
Practical Scenarios with Loops
16. Which of the following loops is best suited for iterating through an array?
a) for
b) while
c) do-while
d) All of the above
Answer: a
17. What is the output of the following code?
java
Copy code
for (int i = 1; i <= 3; i++) {
[Link](i * i + " ");
}
a) 1 4 9
b) 1 2 3
c) 1 8 27
d) None of the above
Answer: a
18. What will the following code print?
java
Copy code
int i = 0;
do {
[Link](i + " ");
i++;
} while (i < 3);
a) 1 2 3
b) 0 1 2
c) 0 1 2 3
d) Infinite loop
Answer: b
19. What is the purpose of the continue statement in loops?
a) Exit the loop immediately.
b) Skip the current iteration and proceed to the next.
c) Restart the loop.
d) None of the above.
Answer: b
20. What will the following code print?
java
Copy code
for (int i = 1; i <= 5; i++) {
[Link](i + " ");
if (i == 3) break;
}
a) 1 2 3
b) 1 2 3 4 5
c) 1 2
d) None of the above
Answer: a
Advanced Concepts with Single Loops
21. Which of the following is the correct syntax for a for-each loop?
a) for (int i : array)
b) for (int i = 0; i < [Link]; i++)
c) for (int i = array)
d) None of the above
Answer: a
22. What will the following code print?
java
Copy code
for (int i = 0; i < 5; i++) {
[Link]("Hello ");
}
a) Hello
b) Hello Hello Hello Hello Hello
c) HelloHelloHelloHelloHello
d) None of the above
Answer: b
23. What is the default value of a boolean variable in Java?
a) true
b) false
c) 0
d) null
Answer: b
24. Which of the following will result in an infinite loop?
a) for (int i = 0; i < 5; i--)
b) while (true)
c) do { ... } while (true);
d) All of the above
Answer: d
25. Which of these loops guarantees at least one iteration?
a) for
b) while
c) do-while
d) None of the above
Answer: c