wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Foundations of Programming

Total questions: 25

Worksheet time: 24mins

Name
Class
Date
1.

Variables & Operators : What is a variable in programming?

a)

A fixed value that cannot be changed

b)

A storage location identified by a memory address

c)

A named storage for a value that can change during program execution

d)

A type of function

2.

Variables & Operators: What type of operator is used to compare two values?

a)

Arithmetic operator

b)

Assignment operator

c)

Logical operator

d)

Comparison operator

3.

Variables & Operators: Which operator is used for addition in Java?

a)

*

b)

/

c)

+

d)

-

4.

Variables & Operators : What will be the value of x after the following code is executed?
int x = 5;

x += 3;

a)

3

b)

5

c)

8

d)

15

5.

Variables & Operators: What is the result of the following expression in Java?
int a = 10;

int b = 20;

int c = a * b + a / b;

a)

10

b)

100

c)

201

d)

204

6.

If Statements & Loops : What does an if statement do?

a)

Repeats a block of code multiple times

b)

Runs a block of code only if a condition is true

c)

Runs a block of code a specific number of times

d)

Terminates a program

7.

If Statements & Loops : What is a loop used for in programming?

a)

To store multiple values

b)

To call functions

c)

To repeat a block of code

d)

To compare values

8.

If Statements & Loops : Which of the following correctly uses an if statement in Java?

a)

if x > 10 then { //code }

b)

if (x > 10) { //code }

c)

if (x > 10): { //code }

d)

if x > 10 { //code }

9.

If Statements & Loops : What is the output of the following code snippet?
int x = 5;

if (x < 10) {

    System.out.println("Less than 10");

}

a)

No output

b)

Less than 10

c)

Greater than 10

d)

Compilation error

10.

If Statements & Loops : How many times will the following loop execute?
for (int i = 0; i < 5; i++) {

    System.out.println(i);

}

a)

4 times

b)

5 times

c)

6 times

d)

Infinite times

11.

Functions : What is a function in programming?

a)

A variable that stores a value

b)

A block of code that performs a specific task and can be called by name

c)

A type of loop that runs indefinitely

d)

A conditional statement

12.

Functions : Which of the following is an example of calling a function in Java?

a)

if (condition)

b)

System.out.print("Hello World")

c)

int x = 10

d)

while (true)

13.

Functions : What is the return type of a function that does not return any value in Java?

a)

int

b)

void

c)

null

d)

float

14.

Functions : Which of the following is the correct way to define a function in Java?

a)

void functionName() { //code }

b)

function functionName() { //code }

c)

def functionName() { //code }

d)

functionName() { //code }

15.

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;

    }

}

a)

15

b)

5

c)

10

d)

Compilation Error

16.

Strings : What is a string in programming?

a)

A type of loop

b)

A collection of characters

c)

A mathematical operator

d)

A conditional statement

17.

Strings : How do you concatenate two strings in Java?

a)

Using the & operator

b)

Using the + operator

c)

Using the concat() method

d)

Both B and C

18.

Strings : What is the result of the following code?
String str = "Hello";

str += " World";

System.out.println(str);

a)

Hello

b)

World

c)

Hello World

d)

Compilation Error

19.

Strings : What will the following code snippet output?
String str = "Hello, World!";

System.out.println(str.length());

a)

12

b)

13

c)

14

d)

15

20.

Strings : What is the output of the following code?
String str = "Java Programming";

System.out.println(str.substring(5, 16));

a)

"Java"

b)

"Programming"

c)

"Progra"

d)

"va Progra"

21.

Arrays/Lists : What is an array (or list) used for in programming?

a)

To store a collection of items in a specific order

b)

To execute a block of code repeatedly

c)

To perform arithmetic operations

d)

To define a function

22.

Arrays/Lists : How do you access an element in an array in Java?

a)

By using the element index value

b)

By using the array's name

c)

By using the element index

d)

By using the length of the array

23.

Arrays/Lists : How do you declare an array of integers in Java?

a)

int array = new int[10];

b)

int[] array = new int[10];

c)

int array[10];

d)

int array = new int();

24.

Arrays/Lists : What will the following code output?
int[] arr = {1, 2, 3, 4, 5};

System.out.println(arr[2]);

a)

1

b)

2

c)

3

d)

4

25.

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 + " ");

}

a)

Apple Banana Cherry

b)

Banana Cherry Apple

c)

Cherry Apple Banana

d)

Apple Cherry Banana