Review for Final Exam Spring 2005 P. Van Hise Part 1 Given the nested if-else structure below, answer questions 1-2



Download 81.67 Kb.
Date29.01.2017
Size81.67 Kb.
#11311
TypeReview
CSC220 Review for Final Exam Spring 2005 P. Van Hise
Part 1
Given the nested if-else structure below, answer questions 1-2.
if (a > 0)

if (b < 0)

x = x + 5;

else


if (a > 5)

x = x + 4;

else

x = x + 3;



else

x = x + 2;




  1. If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?

    1. 0

    2. 2

    3. 3

    4. 4

    5. 5




  1. If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?

    1. 0

    2. 2

    3. 3

    4. 4

    5. 5




  1. The break statement does which of the following?

    1. ends the program

    2. transfers control out of the current control structure such as a loop or switch statement

    3. ends the current line of output, returning the cursor

    4. denotes the ending of a switch statement

    5. indicates the end of line when using System.out.print

4) If x is an int where x = 0, what will x be after the following loop terminates?

while (x < 100)

x *= 2;


a) 2

    1. 64

    2. 100

    3. 128

    4. None of the above, this is an infinite loop

5) How many times will the following loop iterate?

int x = 10;

while (x > 0)

{

System.out.println(x);



x--;

}


  1. 0 times

  2. 1 time

  3. 9 times

  4. 10 times

  5. 11 times

6) Given that s is a String, what does the following loop do?

for (int j = s.length( ); j > 0; j--)

System.out.print(s.charAt(j-1));


  1. it prints s out backwards

  2. it prints s out forwards

  3. it prints s out backwards after skipping the last character

  4. it prints s out backwards but does not print the 0th character

  5. it yields a run-time error because there is no character at s.charAt(j-1) for j = 0

7) The following nested loop structure will execute the inner most statement (x++) how many times?

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

for (int k = 100; k > 0; k--)

x++;


  1. 100

  2. 200

  3. 10,000

  4. 20,000

  5. 1,000,000




    8) In order to create a constant, you would use which of the following Java reserved words?

  1. private

  2. static

  3. int

  4. final

  5. class

Part 2




  1. An exception can produce a “call stack trace” which lists

    1. the active methods in the order that they were invoked

    2. the active methods in the opposite order that they were invoked

    3. the values of all instance data of the object where the exception was raised

    4. the values of all instance data of the object where the exception was raised and all local variables and parameters of the method where the exception was raised

    5. the name of the exception thrown



  1. A finally clause will execute

    1. only if the try statement that precedes it does not throw an exception

    2. only if the try statement that precedes it throws an exception that is caught

    3. only if the try statement that precedes it throws an exception that is not caught

    4. only if the try statement that precedes it throws an exception, whether it is caught or not

    5. in any circumstance




  1. Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException?

    1. str.length( )

    2. str.charAt(2);

    3. str.replace(‘a’, ‘A’);

    4. str.equals(str);

    5. any of the above could throw a StringIndexOutOfBoundsException


  1. Character streams manage

    1. byte-sized data

    2. binary data

    3. Unicode characters

    4. ASCII characters

    5. compressed data


  1. The Scanner class provides an abstraction for input operations by

    1. using try and catch statements to catch any IOException instead of throwing the Exception elsewhere

    2. parsing input lines into individual tokens

    3. performing conversion operations from String to the appropriate type as specified in the Scanner message

    4. inputting from the standard input stream if create is called using System.in

    5. all of the above



  1. The difference between a checked and an unchecked exception is:

    1. checked exceptions need not be listed in a throws clause

    2. unchecked exceptions must be listed in a throws clause

    3. neither kind of exception follows the rules of exception propagation

    4. an unchecked exception requires no throws clause

    5. a checked exception always must be caught by a try block; an unchecked exception does not

public int question(int x, int y)

{

if (x == y) return 0;



else return question(x-1, y) + 1;

}
7) If the method is called as question(8, 3), what is returned?



  1. 11

  2. 8

  3. 5

  4. 3

  5. 24

8) Calling this method will result in infinite recursion if which condition below is initially true?



    1. (x = = y)

    2. (x != y)

    3. (x > y)

    4. (x < y)

    5. (x = = 0 && y != 0)

9) The following method should:


return true if the int parameter is even and either positive or 0, and false otherwise.
Which set of code should you use to replace … so that the method works appropriately?
public boolean question(int x) { … }

a) if (x = = 0) return true;else if (x < 0) return false;else return question(x – 1);

b) if (x = = 0) return false;else if (x < 0) return true;else return question(x – 1);

c) if (x = = 0) return true;else if (x < 0) return false;else return question(x – 2);

d) if (x = = 0) return false;else if (x < 0) return true;else return question(x – 2);

e) return(x = = 0);


10) What does the following method compute? Assume the method is called initially with i = 0

public int question(String a, char b, int i)

{

if (i = = a.length( )) return 0;



else if (b = = a.charAt(i)) return question(a, b, i+1) + 1;

else return question(a, b, i+1);

}
a) The length of String a

b) The length of String a concatenated with char b

c) The number of times char b appears in String a

d) Returns 1 if char b appears in String a at least once, and 0 otherwise

e) The char which appears at location i in String a
Assume that int[ ] a = {6, 2, 4, 6, 2, 1, 6, 2, 5} and consider the two recursive methods below, foo and bar.
public int foo(int[ ] a, int b, int j)

{

if (j < a.length)



if (a[j] != b) return foo (a, b, j+1);

else return foo (a, b, j+1) + 1;

else return 0;

}
public int bar(int[ ] a, int j)

{

if (j < a.length)



return a[ j ] + bar(a, j+1);

else return 0;

}

11) What is the result of calling foo(a, 2, 0);?



a) 0

b) 1


c) 2

d) 3


e) 4
12) What is the result of calling bar(a, 0);?
a) 0

b) 5


c) 6

d) 12


e) 34
13) What is the result of bar(a, 8);?

a) 0


b) 5

c) 6


d) 12

e) 34
14) What does the following recursive method determine?

public boolean question(int[ ]a, int[ ] b, int j)

{

if (j = = a.length) return false;



else if (j = = b.length) return true;

else return question(a, b, j+1);

}

a) Returns true if a and b are equal in size, false otherwise



b) Returns true if a is larger than b, false otherwise

c) Returns true if b is larger than a, false otherwise

d) Returns true if a and b have no elements

e) Returns the length of array a + length of array b

Part 3
For questions 1 – 4, use the following partial class definitions:

public class A1

{

public int x;



private int y;

protected int z;

}
public class A2 extends A1



{

protected int a;

private int b;

}



public class A3 extends A2

{

private int q;



}


  1. Which of the following is true with respect to A1, A2 and A3?

    1. A1 is a subclass of A2 and A2 is a subclass of A3

    2. A3 is a subclass of A2 and A2 is a subclass of A1

    3. A1 and A2 are both subclasses of A3

    4. A2 and A3 are both subclasses of A1

    5. A1, A2 and A3 are all subclasses of the class A




  1. Which of the following lists of instance data are accessible in class A2?

    1. x, y, z, a, b

    2. x, y, z, a

    3. x, z, a, b

    4. z, a, b

    5. a, b




  1. Which of the following lists of instance data are accessible in A3?

    1. x, y, z, a, b, q

    2. a, b, q

    3. a, q

    4. x, z, a, q

    5. x, a, q




  1. Which of the following is true regarding the use of instance data y of class A1?

    1. it is accessible in A1, A2 and A3

    2. it is accessible in A1 and A2

    3. it is accessible only in A1

    4. it is accessible only in A3

    5. it is not accessible to any of the three classes




  1. The instruction super( ); does which of the following?

    1. calls the method super as defined in the current class

    2. calls the method super as defined in the current class’ parent class

    3. calls the method super as defined in java.lang

    4. calls the constructor as defined in the current class

    5. calls the constructor as defined in the current class’ parent class




  1. Inheritance through an extended (derived) class supports which of the following concepts?

    1. interfaces

    2. modulary

    3. information hiding

    4. code reuse

    5. correctness




  1. Aside from permitting inheritance, the visibility modifier protected is also used to

    1. permit access to the protected item by any class defined in the same package

    2. permit access to the protected item by any static class

    3. permit access to the protected item by any parent class

    4. ensure that the class can not throw a NullPointerException

    5. define abstract elements of an interface




  1. Which of the following is an example of multiple inheritance?

    1. a computer can be a mainframe or a PC

    2. a PC can be a desktop or a laptop

    3. a laptop is both a PC and a portable device

    4. a portable device is a lightweight device

    5. Macintosh and IBM PC are both types of PCs




  1. Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by

    1. importing classes

    2. implementing interfaces

    3. overriding parent class methods

    4. creating aliases

    5. using public rather than protected or private modifiers




  1. If a programmer writes a class wanting it to be extended by another programmer, then this programmer must

    1. change private methods and instance data to be protected

    2. change public methods and instance data to be protected

    3. change all methods to be protected

    4. change the class to be protected

    5. none of the above, the programmer does not have to change anything




  1. All classes in Java are directly or indirectly subclasses of the _______ class.

    1. Wrapper

    2. String

    3. Reference

    4. this

    5. Object




  1. Which of the following is not a method of the Object class?

    1. clone

    2. compareTo

    3. equals

    4. toString




  1. Which of the following is true regarding Java classes?

    1. All classes must have 1 parent but may have any number of children (derived or extended) classes.

    2. All classes must have 1 child (derived or extended) class but may have any number of parent classes.

    3. All classes must have 1 parent class and may have a single child (derived or extended) class.

    4. All classes can have any number (0 or more) of parent classes and any number of children (derived or extended) classes.

    5. All classes can have either 0 or 1 parent class and any number of children (derived or extended) classes.


  1. In order to determine the type that a polymorphic variable refers to, the decision is made

    1. by the programmer at the time the program is written

    2. by the compiler at compile time

    3. by the operating system when the program is loaded into memory

    4. by the Java run-time environment at run time

    5. by the user at run time

For questions 15-17, assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where … are the required parameters for the constructors:

Person p = new Person(…);

int m1 = p.getMoney( ); // assignment 1

p = new Student(…);

int m2 = p.getMoney( ); // assignment 2

if (m2 < 100000) p = new Employee(…);

else if (m1 > 50000) p = new Retired(…);

int m3 = p.getMoney( ); // assignment 3


  1. The reference to getMoney( ) in assignment 1 is to the class

    1. Person

    2. Student

    3. Employee

    4. Retired

    5. none of the above, this cannot be determined by examining the code

16) The reference to getMoney( ) in assignment 2 is to the class



  1. Person

  2. Student

  3. Employee

  4. Retired

  5. none of the above, this cannot be determined by examining the code

17) The reference to getMoney( ) in assignment 3 is to the class



  1. Person

  2. Student

  3. Employee

  4. Retired

  5. none of the above, this cannot be determined by examining the code



    18) Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name.

    1. equals (String)

    2. toUpperCase (String)

    3. sqrt (Math)

    4. format (DecimalFormat)

    5. paint (Applet)




    19) An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?

  1. inner

  2. i

  3. private

  4. this

  5. static

20) Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by



  1. importing classes

  2. implementing interfaces

  3. overriding parent class methods

  4. creating aliases

  5. using public rather than protected or private modifiers

21) In which phase of program development would you expect the programmer(s) to create the pseudocode?

a) Software requirements

b) Software design

c) Software implementation

d) Software testing

e) Could occur in any of the above

Part 4
For questions 1-4, assume values is an int array that is currently filled to capacity, with the following values:






  1. What is returned by values[3]?

    1. 9

    2. 12

    3. 2

    4. 6

    5. 3




  1. What is the value of values.length?

    1. 0

    2. 5

    3. 6

    4. 7

    5. 18




  1. What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0.

for (j=0; j < list.length; j++)

if (list[j] < temp) c++;


  1. It finds the smallest value and stores it in temp

  2. It finds the largest value and stores it in temp

  3. It counts the number of elements equal to the smallest value in list

  4. It counts the number of elements in list that are less than temp

  5. It sorts the values in list to be in ascending order

An int array stores the following values. Use the array to answer questions 4 – 7






  1. Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?

    1. 9, 4, 12, 2, 6, 8, 18

    2. 4, 9, 12, 2, 6, 8, 18

    3. 2, 4, 12, 9, 6, 8, 18

    4. 2, 4, 6, 8, 9, 12, 18

    5. 2, 4, 9, 12, 6, 8, 18




  1. Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?

    1. 9, 4, 12, 2, 6, 8, 18

    2. 2, 4, 9, 6, 12, 8, 18

    3. 2, 4, 12, 9, 6, 8, 18

    4. 2, 4, 6, 8, 9, 12, 18

    5. 2, 4, 12, 6, 8, 9, 18



  1. How many passes will it take in all for Selection Sort to sort this array?

    1. 2

    2. 4

    3. 5

    4. 6

    5. 7

7) What are the main programming mechanisms that constitute object-oriented programming?



  1. Encapsulation, inheritance, polymorphism

  2. Encapsulation, abstraction, inheritance

  3. Inheritance, polymorphism, recursion

  4. Polymorphism, recursion, abstraction

  5. None of the above

8) What is printed by the following code? Consider the polymorphic invocation.

public class Inherit

{
class Figure

{

void display( )



{

System.out.println("Figure");

}

}
class Rectangle extends Figure



{

void display( )

{

System.out.println("Rectangle");



}

}
class Box extends Figure

{

void display( )



{

System.out.println("Box");

}

}
Inherit( )



{

Figure f = new Figure( );

Rectangle r = new Rectangle( );

Box b = new Box( );

f.display( );

f = r;


f.display( );

f = b;


f.display( );

}
public static void main(String[ ] args)

{

new Inherit( );



}

}


  1. Figure

Rectangle

Box



  1. Rectangle

Box


  1. Figure

Figure

Figure



  1. Syntax error. This code won't compile or execute




  1. None of the above

9) One way polymorphism is achieved is by



  1. extending

  2. overriding

  3. embedding

  4. abstraction

  5. encapsulation

10) Which of these methods will sort an array of floats into ascending order?






void arrange(double[ ] ary)

{

for (int n=0; n

for (int k=n; k

if (ary[n] > ary[k])

{

double x = ary[n];



ary[n] = ary[k];

ary[k] = x;

}

}




void arrange(double [ ] ary)

{

for (int n=0; n

for (int k=n; k

if (ary[n] < ary[k])

{

double x = ary[n];



ary[n] = ary[k];

ary[k] = x;

}

}




void arrange(double [ ] ary)

{

for (int n=1; n<=ary.length; n++)



for (int k=n; k

if (ary[n] > ary[k])

{

double x = ary[n];



ary[n] = ary[k];

ary[k] = x;

}

}




void arrange(double [ ] ary)

{

for (int n=0; n

for (int k=n; k

if (ary[n] > ary[k])

double x = ary[n];

ary[n] = ary[k];

ary[k] = x;

}


  1. None of the above

ANSWERS:
Part 1
1.C 2 B 3 B 4 D 5 D 6 A 7 C 8D
Part 2
1. B Explanation: The call stack trace provides the names of the methods as stored on the run-time stack. The method names are removed from the stack in the opposite order that they were placed, that is, the earliest method was placed there first, the next method second, and so forth so that the most recently invoked method is the last item on the stack, so it is the first one removed. The stack trace then displays all active methods in the opposite order that they were called (most recent first).
2 E 3 B
4.C Explanation: Character streams are used to manage 16-bit Unicode characters. This differs from a byte stream that is used to manage any kind of byte-sized data, including ASCII characters and binary data of other types.

5 E Explanation: Input into a Java program is difficult because it requires a lot of overhead. The Scanner class implements all of that overhead so that the programmer does not have to see it. Thus, Scanner is an abstraction for performing input operations without the details. These details include importing java.io classes, handling IOExceptions in some way, inputting from the standard input stream, dividing the input into individual tokens and converting each token as needed into the requesting form.


6 D Explanation: A checked exception must either be caught or it must be listed in a throws clause. An unchecked exception requires no throws clause. Both kinds of exceptions follow the rules of exception propagation.
7 C 8 D 9 C 10 C 11 D 12 E 13 B 14 B
Part 3
1 B 2 C 3 D 4 C 5 E 6 D
7 A Explanation: The visibility modifier protected is used to control access to the item in a protected (guarded) manner. The protection is that access is restricted to the current class (like private items), classes in the same package, or extended classes of this class.
8 C Explanation: Multiple inheritance means that a given class inherits from more than one parent class. Of those listed above, a laptop computer inherits properties from both PCs and portable devices. The answers in a, b and e are all examples of single inheritance where a class has at least two children (in a, computer has children mainframe and PC, in b, PC has children desktop and laptop, in e, PC has children Macintosh and IBM PC). Answer d denotes a property of a class.

9 B 10 A 11 E 12 B 13 A 14 D


15 A Explanation: At this point of the program, p is a Person, and so getMoney is a reference to Person’s getMoney method.
16 B Explanation: At this point of the program, p is a Student, and so getMoney is a reference to Student’s getMoney method.
17 E 18 C 19 D 20 B
21 B Answer: Pseudocode is a description of an algorithm written in an English-like way rather than in a specific programming language. This is part of the program’s design. In the implementation phase, the programmer(s) translates the pseudocode into the programming language being used.
Part 4
1 C 2 D 3 D 4 C 5 C 6 D 7 A 8 A 9 B 10 A



Download 81.67 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page