Sample Exam 1 questions
Which of the following operators is a relational operator?
!=
How many times will the output line be printed in the following code snippet? for(int x=0; x<3; x++){ for(int y=0; y<=3; y++){ System.out.println(x*y); } }
12
How many times does the code snippet below display "Hello"? int i=0; while(i != 15) { System.out.println("Hello"); i++; }
15 times
What is the value of integer variable cnt at the end of the program? String str = "Iowa State University Ivy College of Business"; int cnt = 0; for (int i = 0; i < str.length(); i++){ char ch = str.charAt(i); if (ch == 'I'){ cnt++; } }
2
Assume the following variable has been declared and given a value as shown: int[][] data = { {9, 17, -4, }, {15, 24, 0}, }; System.out.println(data.length + " " + data[0].length);
2 3
What is the output of the code snippet given below? String s = "12345"; int i=1; while (i < 5){ System.out.print(s.substring(i,i+1)); i += 2; }
24
What is the output of the following code snippet? int x = 50; if (x > 100) { x++; }else { x--; } System.out.println(x);
49
Consider the following code snippet: int[][] arr = { {1, 2, 3, 0} , {4, 5, 6, 0} , {0, 0, 0, 0} }; int[][] arr2 = arr; System.out.println(arr2[2][1] + arr2[1][2]);
6
What mechanism is used by a class that is designed to collect and store a growing set of values?
An array list or array.
What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for( String s: names){ System.out.print(s + ","); }
Ann, Tony
Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); }
for (String str : arr)
Write nested for loops to print the following. A AA AAA AAAA
for(int row=1; row<=4; row++){ for(int col = 1; col <= row; col++){ System.out.print("A"); } System.out.println(); }
Which of the following expressions represents a legal way of checking whether a value assigned to the number variable falls between 50 and 100 inclusive?
if (number >= 50 && number <= 100)
Which code snippet will always output "Yes!" when s1 and s2 are two strings the contain the same sequence of characters?
if (s1.equals(s2)) System.out.println("Yes!");
Judging by the name of the method, which of the following methods would most likely be a mutator method?
updateDeposit
Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for(int current = 1; current < values.length; current++){ if ( _________________ ){ max = values[current]; } }
values[current] > max