You are on page 1of 8

Quiz 1 Sectiunea 6

1. Which of the following statements is a valid array declaration?


int number();
float average[]; (*)
double[] marks; (*)
counter int[];
2.The following array declaration is valid: int[] y = new int[5];

True (*)

False

3. Which of the following declares a one dimensional array named "score" of type int that can hold
9 values?
int score;
int[] score;
int[] score=new int[9]; (*)
int score=new int[9];
4. Which of the following declares and initializes a two dimensional array?
int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
int[] array={{1,1,1},{1,1,1},{1,1,1}};
int[][] array={1,1,1},{1,1,1},{1,1,1};
int[][] array={1,1,1,1,1,1,1,1,1};
5. Which of the following declares and initializes a one dimensional array named words of size 10
so that all entries can be Strings?
String words=new String[10];
char words=new char[10];
char[] words=new char[10];
String[] words=new String[10]; (*)
6.

What is the output of the following segment of code?

222220
0 (*)
220
2
This code does not compile.

7. What is the output of the following segment of code?

1286864
643432
262423242322
666666 (*)
This code does not compile.

8. Which of the following declares and initializes a two dimensional array named values with 2
rows and 3 columns where each element is a reference to an Object?
String[][] values={"apples","oranges","pears"};
String[][] values=new String[3][2];
Sectiunea 6

String[][] values=new String[2][3]; (*)


String[][] values;
9. Which of the following declares and initializes a two dimensional array where each element is a
reference type?
String words=new String[10];
char[][] words;
char[][] words=new char[10][4];
String[][] words=new String[10][3]; (*)
10.
Which of the following statements print every element of the one dimensional array prices
to the screen?
for(int i=0; i < prices.length; i++){System.out.println(prices[i]);} (*)
System.out.println(prices.length);
for(int i=1; i <= prices.length; i++){System.out.println(prices[i]);}
for(int i=0; i <= prices.length; i++){System.out.println(prices[i]);}
11. What is the output of the following segment of code?

753
6
7766554433221
7531 (*)
This code does not compile.
12.
The following creates a reference in memory named y that can refer to five different integers
via an index. True or false? int[] y = new int[5];
True (*)
False
13. The following creates a reference in memory named z that can refer to seven different doubles
via an index. True or false? double z[] = new double[7];
True (*)
False
14. What is the output of the following segment of code if the command line arguments are "apples
oranges pears"?
apples
pears (*)
oranges
args
This code doesn't compile.
15. What is the output of the following segment of code if the command line arguments are "apples
oranges pears"?
0
1
2
3 (*)
This code does not compile.
16. What will be the content of array variable table after executing the following code?

Sectiunea 6

000
000
000
100
010
0 0 1 (*)
100
110
111
001
010
100
17.What is the output of the following segment of code?

1286864 (*)
643432
262423242322
666666
This code does not compile.

18. After execution of the following statement, which of the following are true?
int number[] = new int[5];
number[0] is undefined
number[4] is null
number[2] is 0 (*)
number.length() is 6
19. The following array declaration is valid. True or false?int x[] = int[10];
False (*)

True

Quiz 2 Sectiunea 6
1. Consider the following code snippet. What is printed?
String river = new String("Hudson"); System.out.println(river.length());
6 (*)
7
8
Hudson
river
2. Which of the following creates a String reference named str and instantiates it?
String str;
str="str";
String s="str";
String str=new String("str"); (*)
3. Declaring and instantiating a String is much like any other type of variable. However, once
instantiated, they are final and cannot be changed. True or false?
True (*)
False
4. Which of the following statements declares a String object called name?
String name; (*)
String name
Sectiunea 6

int name;
double name;

5. Suppose that s1 and s2 are two strings. Which of the statements or expressions are valid?
String s3 = s1 + s2; (*)
String s3 = s1 - s2;
s1 <= s2
s1.compareTo(s2); (*)
int m = s1.length(); (*)
6. Consider the following code snippet. What is printed?

PoliiPolii (*)
Polii
auaacauaac
auaac
ArrayIndexOutofBoundsException is thrown

7. What will the following code segment output?

"\\\\\"
\"\\\\\"
"\\" (*)

""\\"
""\"
""\

"\\\"
8. What will the following code segment output?

" (*)
"""\
""
""\
""
9. The following program prints "Equal". True or false?

True (*)
False

10. Which of the following creates a String named string?


char string;
String s;
String string; (*)
String String;
Sectiunea 6

String char;

11. Given the code, which of the following would equate to true?
String s1 = "yes";
String s2 = "yes";
String s3 = new String(s1);
s1 == s2 (*)
s1 = s2
s3 == s1
s1.equals(s2) (*)
s3.equals(s1) (*)
12. The String methods equals and compareTo perform the exact same function. True or false?
True
False (*)
13. The == operator can be used to compare two String objects. The result is always true if the two
strings are identical. True or false?
True
False (*)
14. The following program prints "Equal". True or false?

True

False (*)

15. Given the code below, which of the following calls are valid?
String s = new String("abc");
s.trim() (*)
s.replace('a', 'A') (*)
s.substring(2) (*)
s.toUpperCase() (*)
s.setCharAt(1,'A')
16. Consider the following code snippet. What is printed?
String ocean = new String("Atlantic Ocean"); System.out.println(ocean.indexOf('a'));
0
2
3 (*)
11
12
17. Consider the following code snippet. What is printed?

55555
87668 (*)
AtlanticPacificIndianArcticSouthern
The code does not compile.
An ArrayIndexOutofBoundsException is thrown.

18. Consider the following code snippet. What is printed?

Sectiunea 6

55555
87658
AtlanticPacificIndianArcticSouthern
The code does not compile.
An ArrayIndexOutofBoundsException is thrown. (*)

19.How would you use the ternary operator to rewrite this if statement?
if (skillLevel > 5)
numberOfEnemies = 10;
else
numberOfEnemies = 5;

numberOfEnemies = ( skillLevel > 5) ? 5 : 10;


numberOfEnemies = ( skillLevel < 5) ? 10 : 5;
numberOfEnemies = ( skillLevel >= 5) ? 5 : 10;
numberOfEnemies = ( skillLevel >= 5) ? 10 : 5;
numberOfEnemies = ( skillLevel > 5) ? 10 : 5; (*)
20. How would you use the ternary operator to rewrite this if statement?
if (gender == "male")
System.out.print("Mr.");
else
System.out.print("Ms.");
System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)
System.out.print( (gender == "male") ? "Ms." : "Mr." );
(gender == "male") ? "Mr." : "Ms." ;
(gender == "male") ? "Ms." : "Mr." ;

Quiz 3 Sectiunea 6
1. Which of the following would give you an array index out of bounds exception?
Misspelling a variable name somewhere in your code.
Refering to an element of an array that is at an index less than the length of the array minus
one.
Using a single equal symbol to compare the value of two integers.
Refering to an element of an array that is at an index greater than the length of that array
minus one. (*)
Unintentionally placing a semicolon directly after initializing a for loop.
2. What exception message indicates that a variable may have been mispelled somewhere in the
program?
variableName cannot be resolved to a variable (*)
Sectiunea 6

method methodName(int) is undefined for the type className


Syntax error, insert ";" to complete statement
All of the Above

3. Which of the following defines an Exception?


A very severe non-fixable problem with interpreting and running your code.
Code that has no errors and therefore runs smothly.
A problem that can be corrected or handled by your code. (*)
An interpreter reading your code.
4. What do exceptions indicate in Java?
The code has considered and dealt with all possible cases.
A mistake was made in your code. (*)
There are no errors in your code.
Exceptions do not indicate anything, their only function is to be thrown.
The code was not written to handle all possible conditions. (*)
5. Which line of code shows the correct way to throw an exception?
new throw Exception("Array index is out of bounds");
throw new Exception("Array index is out of bounds"); (*)
throw Exception("Array index is out of bounds");
throws new Exception("Array index is out of bounds");
6. What does the interpreter look for when an exception is thrown?
It does not look for anything. It just keeps reading through your code.
It does not look for anything. It stops interpreting your code.
The end of the code.
A catch statement in the code. (*)
7. Which of the following would be a correct way to handle an index out of bounds exception?
Throw the exception and catch it. In the catch, set the index to the index of the array closest
to the one that was out of bounds. (*)
Do nothing, it will fix itself.
Throw the exception that prints out an error message. There is no need to have the catch
handle the exception if it has already been thrown.
Rewrite your code to avoid the exception by not permititng the use of an index that is not
inside the array. (*)
8. A computer company has one million dollars to give as a bonus to the employees, and they wish
to distribute it evenly amongst them.
The company writes a program to calculate the amount each employee receives, given the number
of employees.
Unfortunately, the employees all went on strike before they heard about the bonus. This means that
the company has zero employees.
What will happen to the program if the company enters 0 into the employment number?
An unfixable error will occur.
Sectiunea 6

The program will calculate that each employee will receive zero dollars because there are
zero employees.
An exception will occur because it is not possible to divide by zero. (*)
The programmers will have proven their worth in the company because without them the
company wrote faulty code.

Sectiunea 6

You might also like