NEW
Font size
WorksheetsFoundations of Programming
Total questions: 25
Worksheet time: 24mins
Variables & Operators : What is a variable in programming?
A fixed value that cannot be changed
A storage location identified by a memory address
A named storage for a value that can change during program execution
A type of function
Variables & Operators: What type of operator is used to compare two values?
Arithmetic operator
Assignment operator
Logical operator
Comparison operator
Variables & Operators: Which operator is used for addition in Java?
*
/
+
-
Variables & Operators : What will be the value of x after the following code is executed?
int x = 5;
x += 3;
3
5
8
15
Variables & Operators: What is the result of the following expression in Java?
int a = 10;
int b = 20;
int c = a * b + a / b;
10
100
201
204
If Statements & Loops : What does an if statement do?
Repeats a block of code multiple times
Runs a block of code only if a condition is true
Runs a block of code a specific number of times
Terminates a program
If Statements & Loops : What is a loop used for in programming?
To store multiple values
To call functions
To repeat a block of code
To compare values
If Statements & Loops : Which of the following correctly uses an if statement in Java?
if x > 10 then { //code }
if (x > 10) { //code }
if (x > 10): { //code }
if x > 10 { //code }
If Statements & Loops : What is the output of the following code snippet?
int x = 5;
if (x < 10) {
System.out.println("Less than 10");
}
No output
Less than 10
Greater than 10
Compilation error
If Statements & Loops : How many times will the following loop execute?
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
4 times
5 times
6 times
Infinite times
Functions : What is a function in programming?
A variable that stores a value
A block of code that performs a specific task and can be called by name
A type of loop that runs indefinitely
A conditional statement
Functions : Which of the following is an example of calling a function in Java?
if (condition)
System.out.print("Hello World")
int x = 10
while (true)
Functions : What is the return type of a function that does not return any value in Java?
int
void
null
float
Functions : Which of the following is the correct way to define a function in Java?
void functionName() { //code }
function functionName() { //code }
def functionName() { //code }
functionName() { //code }
Functions : What is the output of the following code snippet?
public class Main {
public static void main(String[] args) {
System.out.println(addNumbers(5, 10));
}
public static int addNumbers(int a, int b) {
return a + b;
}
}
15
5
10
Compilation Error
Strings : What is a string in programming?
A type of loop
A collection of characters
A mathematical operator
A conditional statement
Strings : How do you concatenate two strings in Java?
Using the & operator
Using the + operator
Using the concat() method
Both B and C
Strings : What is the result of the following code?
String str = "Hello";
str += " World";
System.out.println(str);
Hello
World
Hello World
Compilation Error
Strings : What will the following code snippet output?
String str = "Hello, World!";
System.out.println(str.length());
12
13
14
15
Strings : What is the output of the following code?
String str = "Java Programming";
System.out.println(str.substring(5, 16));
"Java"
"Programming"
"Progra"
"va Progra"
Arrays/Lists : What is an array (or list) used for in programming?
To store a collection of items in a specific order
To execute a block of code repeatedly
To perform arithmetic operations
To define a function
Arrays/Lists : How do you access an element in an array in Java?
By using the element index value
By using the array's name
By using the element index
By using the length of the array
Arrays/Lists : How do you declare an array of integers in Java?
int array = new int[10];
int[] array = new int[10];
int array[10];
int array = new int();
Arrays/Lists : What will the following code output?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[2]);
1
2
3
4
Arrays/Lists : What will be the output of the following code snippet?
String[] fruits = {"Apple", "Banana", "Cherry"};
for (String fruit : fruits) {
System.out.print(fruit + " ");
}
Apple Banana Cherry
Banana Cherry Apple
Cherry Apple Banana
Apple Cherry Banana
