Iterations

Practice Questions

Computer Science › Iterations

Questions
6
1

int var=0;

int array\[4\];

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

{

var=var+1;

}

What is the value of var?

2

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

}

3

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?

4

#include

using namespace std;

int main()

{

int i=0;

int sum =0;

for(i;i<4;i+=2)

{

sum=sum+i;

i--;

}

return 0;

}

What is the value of i and sum?

5

Suppose you are given the following lines of code (and x is some initialized integer):

int k = arr.length;

for (int i = -1; i <k-2; i++)

{

if (arr\[i+2\] < x)

System.out.print("Hello");

}

What is the maximum number of times "Hello" print?

6

public static boolean remove(int[] arr, int val) {

boolean found = false;

int i;

for(i = 0; i < arr.length && !found; i++) {

if(arr[i] == val) {

found = true;

}

}

// START

if(found) {

for(int j = i; j < arr.length;j++) {

arr[j - 1] = arr[j];

}

arr[arr.length - 1] = 0;

}

// END

return found;

}

What does the code between // START and // END do?

Return to subject