You are on page 1of 5

CSCI-185 Programming II Assessment Test

27 Multiple Choices x 4 = 108 Name _____________________ Date ________

1) In Java, array indexes always begin at ________________ .

a) -1
b) 0
c) 1
d) 2
e) you can declare an array to have any indexes you choose

2) Which of the following statements best describes this line of code?

int[] numbers = new int[50];

a) this is the declaration and initialization of an array that holds 50 integers


b) this is a declaration and initialization of an array that holds 49 integers
c) this is a declaration and initialization of an integer array that will hold integers smaller than 50
d) this is a declaration and initialization of an integer array that will hold integers smaller than 49
e) none of the above is correct

3) Which of the statements is true about the following code snippet?

int[] array = new int[25];


array[25] = 2;

a) The integer value 2 will be assigned to the last index in the array.
b) The integer value 25 will be assigned to the second index in the array.
c) The integer value 25 will be assigned to the third value in the array.
d) This code will result in a compile-time error.
e) This code will result in a run-time error.

4) What will be the output of the following code snippet?

int[] array = new int[25];


System.out.println(array.length);

a) 26
b) 24
c) 25
d) This code will result in a compile time error.
e) This code will result in a run time error.

5) Which of the following array declarations are invalid?

a) int[] grades = new int[5];


b) int grades[] = new int[5];
c) int[] grades = { 91, 83, 42, 100, 77 };
d) all of the above are valid
e) none of the above are valid

1
6) All methods (with the exception of constructors) must specify a return type. What is the return type for a method
that does not return any values?

a) int
b) void
c) public
d) double
e) none of the above

7) If a service is so complex that it cannot be reasonably be implemented using one method, it is often helpful to
decompose it to make use of ________________ support methods.

a) static
b) aggregate
c) private
d) public
e) final

8) A special method that is invoked to set up an object during instantiation is called a ___________________.

a) new method
b) dot operator
c) creator
d) destructor
e) constructor

9) Which of the following logical operators has the highest precedence?

a) !
b) &&
c) ||
d) <
e) >

10) What happens if a case in a switch statement does not end with a break statement?

a) The program will not compile.


b) The switch statement will never execute.
c) It will cause an infinite loop.
d) The case will never be executed.
e) The switch statement will execute the next case statement as well.

11) Suppose we want to condition an if statement on whether two String objects, referenced by stringOne and
stringTwo, are the same. Which of the following is the correct way to achieve this?

a) if(stringOne == stringTwo)
b) if(stringOne.compareTo(stringTwo))
c) if(stringOne.equals(stringTwo))
d) if(stringOne != stringTwo)
e) if(stringOne === stringTwo)

2
12) In order to make a variable a constant which modifier must be included in its declaration?

a) public
b) static
c) void
d) private
e) final

13) Consider the expression:

result = 15 % 4;

What value stored in result after this line is executed?

a) 0
b) 1
c) 2
d) 3
e) 4

14) Which of the following lines allows a programmer to use the Scanner class in a Java program?

a) import java.util.Scanner;
b) using Scanner;
c) include Scanner;
d) include java.util.Scanner;
e) any of the above will allow the programmer to use the Scanner class

15) Consider the following snippet of code:

System.out.println("30 plus 25 is " + 30 + 25);

What is printed by this line?

a) 30 plus 25 is 55
b) 30 plus 25 is 30
c) 30 plus 25 is 25
d) 30 plus 25 is 3025
e) this snippet of code will result in a compiler error

16) The _____________ of an object define it define its potential behaviors.

a) attributes
b) white spaces
c) methods
d) variables
e) name

17) The Java compiler translates Java source code into _____________ .

a) Java bytecode
b) C++
c) assembly code
d) machine code
e) an object-oriented language

3
18) A _____________ diagram helps us visualize the contents of and relationships among the classes of a program.

a) class and object


b) UML
c) object-oriented
d) public
e) private

19) Which of the following object-oriented principles refers to the fact that an object should have its data guarded
from inappropriate access?

a) encapsulation
b) inheritance
c) polymorphism
d) instance variables
e) methods

20) When applied to instance variables, the ________________ visibility modifier enforces encapsulation.

a) static
b) final
c) public
d) private
e) none of the above

21) Which of the following method headers is most likely a header for a mutator method?

a) public int getAge()


b) public double computeSalary()
c) public Person()
d) public void setAge(int newAge)
e) none of these are headers for a mutator method

22) Which of the following is a true statement?

a) Arrays are passed as parameters to methods like primitive types.


b) Arrays are passed as parameters to methods like object types.
c) Arrays cannot be passed as parameters to methods.
d) All of the above are true.
e) None of the above are true.

23) Suppose we have an array of String objects identified by the variable names. Which of the following for
loops will not correctly process each element in the array.

a) for(int i = 0; i < names.length; i++)


b) for(String name : names)
c) for(int i = 0; i < names.length(); i++)
d) none of these will correctly process each element
e) all of these will correctly process each element

24) Which of the following statements will assign the first command-line argument sent into a Java program to a
variable called argument?

a) argument = System.getFirstArgument();
b) argument = System.getArgument[1];
c) argument = System.getArgument[0];
d) argument = args[0];
e) argument = args[1];
4
25) Which of the following is a valid declaration for a two-dimensional array?

a) int[][] matrix;
b) int[2] matrix;
c) int[]** matrix;
d) int[] matrix;
e) none of these are correct

26) Which of the following lines of code accesses the second element of the first array in a two-dimensional array
of integers, numbers, and stores the result in a variable called num?

a) num = numbers[1][2];
b) num = numbers[0][1];
c) num = numbers.getElement(1, 2);
d) num = numbers.getElement(0, 1);
e) none of the above are correct

27) Every Java array is a(n) _________________, so it is possible to use a foreach loop to process each element in
the array.

a) object
b) class
c) operator
d) none of the above
e) iterator

You might also like