Programming Constructs

Practice Questions

Computer Science › Programming Constructs

Page 1 of 12
10 of 112
1

Which of the following is a recursive factorial function?

Recall that an example of a factorial is:

2

Which of the following is a recursive factorial function?

Recall that an example of a factorial is:

3

int lairotcaf (int n) {

if (n <= 1){

return 1

}

temp = n * lairotcaf(n-1)

return temp;

}

int num;

cin >> num;

``

if (num >= 0){

lairotcaf(n)

} else {

cout << "must use number greater or equal to zero"< endl;

}

What is returned by lairotcaf(5)?

4

int lairotcaf (int n) {

if (n <= 1){

return 1

}

temp = n * lairotcaf(n-1)

return temp;

}

int num;

cin >> num;

``

if (num >= 0){

lairotcaf(n)

} else {

cout << "must use number greater or equal to zero"< endl;

}

What is returned by lairotcaf(5)?

5

int var=0;

int array\[4\];

for (int j=0; j<=4;j++)

{

var=var+1;

}

What is the value of var?

6

int var=0;

int array\[4\];

for (int j=0; j<=4;j++)

{

var=var+1;

}

What is the value of var?

7

True or False.

This code snippet will iterate 5 times.

ArrayList<String> arrList = new ArrayList<String>();

arrList.add("string0");

arrList.add("string1");

arrList.add("string2");

arrList.add("string3");

arrList.add("string4");

for (int i = 0; i < arrList.size(); i++) {

System.out.println(arrList.get(i));

}

8

Suppose you have the following code:

public static void main(String\[\] args) {

int a =2;

if (a%2==0)

System.out.println("Hello World");

else

System.out.println("Hi");

}

If the main method is called, what will be printed?

9

True or False.

This code snippet will iterate 5 times.

ArrayList<String> arrList = new ArrayList<String>();

arrList.add("string0");

arrList.add("string1");

arrList.add("string2");

arrList.add("string3");

arrList.add("string4");

for (int i = 0; i < arrList.size(); i++) {

System.out.println(arrList.get(i));

}

10

Suppose you have the following code:

public static void main(String\[\] args) {

int a =2;

if (a%2==0)

System.out.println("Hello World");

else

System.out.println("Hi");

}

If the main method is called, what will be printed?

Page 1 of 12
Return to subject