Fundamentals of Programming
Homework (10 Marks)
Section 1: Multiple Choice Questions (5 Questions, 0.5 marks each)
1. Which data type can be used in the condition of an if statement?
a) int;
b) String;
c) boolean;
d) char;
2. What will be the output of this code?
int x = 3;
if (x > 2)
if (x < 4)
[Link]("A");
else
[Link]("B");
a) A
b) B
c) Nothing
d) Error
3. How many else blocks can be used with one if statement in Java?
a) 0
b)1
c)2
d) Unlimited
4. How many times will the following loop execute?
for (int i = 0; i < 5; i++) {
[Link](i);
}
a) 4
b) 5
c) 6
d) infinite
5. What will be the output of this loop?
for (int i = 0; i < 10; i += 3) {
[Link](i + " ");
}
a) 0 1 2 3 4 5 6 7 8 9
b) 0 3 6 9
c) 3 6 9
d) 0 2 4 6 8
Section 2: True or False (5 Questions, 0.5 marks each)
1. The == operator compares the contents of two strings. =F
2. The if statement must always have an else block..=F
3. Curly braces {} are mandatory for single-line if or else blocks.=F
4. A for loop is always the best choice when the number of iterations is known.=T
5. The else block will execute if the if condition is true.=F
Section 3: Programming Task (5 Marks)
Question: Write a Java program that use a loop to print numbers from 10 down to 1.
public class Countdown {
public static void main(String[] args) {
for (int i = 10; i >= 1; i--) {
[Link](i);
}
}