CS221 Test 1

Ace your homework & exams now with Quizwiz!

Which words are printed by the following piece of code? Assume that collect is a collection of strings which implements the Iterator interface and that it it empty prior to this code piece. collect.add("three"); collect.add("six"); collect.add("nine") collect.add("ten"); Iterator itr = collect.iterator(); //print all words with at least 4 letters while ( itr.hasNext() ) { if ( itr.next().length() > 4 { System.out.println(itr.next()); } }

"six" and "ten"

What will be displayed by the call print("*", 4) based on the following implementation? public static void print(String symbol, int num) { while (num > 0) { System.out.println(symbol); num--; } }

* * * *

Parentheses do not come alone, but are used to in expressions, such as mathematical computations or program code. Select the expressions which are balanced. - 3 + a * b - while ( count < size) { if (count % 2 == 0) { sum = sum+count*count; else { sum = sum +count } } - sidelength = Math.sqrt (Math.pow(x,2) + Math.pow(y,2) - ( [ (a + 10 ) * (12 - b) ]/4.3)

- ( [ (a + 10 ) * (12 - b) ]/4.3) - 3 + a * b

Which of the following statements are true? Check all that apply. - A protected member in the superclass is inherited and accessible by the subclass. - A private member in the superclass is inherited and accessible by the subclass. - A package member in the superclass is inherited and accessible by the subclass. - A public member in the superclass is inherited and accessible by the subclass.

- A protected member in the superclass is inherited and accessible by the subclass. - A public member in the superclass is inherited and accessible by the subclass.

Which of the following statements are true? Check all true statements. - abstract can appear in the front of an attribute declaration - An abstract class cannot be final. - abstract can appear in the front of a method - abstract can appear in the front of a class

- An abstract class cannot be final. - abstract can appear in the front of a method - abstract can appear in the front of a class

Which of the following are true statements? - Java is the only language which includes the 'generic' concept. - Classes may be specified with more than on generic type (e.g. HashMap) - Classes with a generic definition (e.g. ArrayList and Stack) come with Java, but programmers cannot write their own. - In a generic statement, the < type > cannot be a primitive type in Java. - Generic classes and methods reduce the amount of repetitive code.

- Generic classes and methods reduce the amount of repetitive code. - In a generic statement, the < type > cannot be a primitive type in Java. - Classes may be specified with more than on generic type (e.g. HashMap)

Which of the following statements is/are true about stacks? - A stack is a good data structure to represent a line of cars at an intersection. - Information is popped (removed) from the stack in the reverse order in which it was pushed (added). - A stack is sometimes referred to as a LIFO (Last-In-First-Out) data structure. - A stack is a good data structure for an execution environment to keep track of where to return to from a method call.

- Information is popped (removed) from the stack in the reverse order in which it was pushed (added). - A stack is sometimes referred to as a LIFO (Last-In-First-Out) data structure. - A stack is a good data structure for an execution environment to keep track of where to return to from a method call.

The Comparator interface has a compare method with the heading int compare (T ob1, T ob2). What is the purpose of the method? - It determines whether obj1 and obj2 are equal. - It determines whether obj1 and obj2 are in a superclass - subclass relationship. - It determines whether obj1 is less than obj2, obj1 is larger than obj2 or whether they are equal. - It determines whether obj1 and obj2 are pointing to the same object.

- It determines whether obj1 is less than obj2, obj1 is larger than obj2 or whether they are equal.

Which of the following statements are true about constructors? - A constructor with parameters of the subclass can only call a constructor with parameters from the superclass. - One constructor from the superclass will always be invoked in the constructor of the subclass. - The default constructor of the subclass can only call the default constructor of the superclass. - If super is used to call the constructor of the superclass, it must be the first statement in the constructor of the subclass.

- One constructor from the superclass will always be invoked in the constructor of the subclass. - If super is used to call the constructor of the superclass, it must be the first statement in the constructor of the subclass.

Assume the variable stack has been declared and instantiated to be an object which implements the stack interface (as given in StackInterface.java). What is true about the pop operation (as defined in StackInterface.java)? - How pop can be called only depends on the implementation, not on the interface. - Pop can be called using stack.pop( ). - Pop can be invoked using T value = stack.pop ( );, and it removes and returns the top element of the stack. - Pop only removes the top element of the stack, but it returns nothing.

- Pop can be called using stack.pop( ). - Pop can be invoked using T value = stack.pop ( );, and it removes and returns the top element of the stack.

Which of the following are correct declarations/instantiations? (Note: Stack is the name of a generic class in Java.) - Stack<int> keepMe = new Stack<int> (); - ArrayList<String> names = new ArrayList<String>(); - Stack<Double> storeMe = new Stack<Double>(); - ArrayList<double> list = new ArrayList<double>();

- Stack<Double> storeMe = new Stack<Double>(); - ArrayList<String> names = new ArrayList<String>();

After invoking method print, what is the value of n in the main method? public static void print(String symbol, int count) { while (count > 0) { System.out.println(symbol); count--; } } public static void main(String[] args) { int n = 3; printStars("*", n); }

3

Class D is a subclass of C, which of the following method header may appear in class B? interface A { int someAction(C c); } class B implements A { { // implementation of someAction in B, assume the implementation is correct } }

public int someAction(C c)

Analyze the following statement Rectangle r = new Rectangle();

r holds a reference to a Rectangle object.

Analyze the following code segment // saved in B.java abstract class A{ public static void sa() { System.out.println("sA"); } public void a() { System.out.println("A"); } } public class B extends A { public void a() { System.out.println("A -> B"); } public static void sa() { System.out.println("sA -> B"); } public static void main(String[] args) { B.sa(); A b = new B(); b.a(); b.sa(); } }

sA -> B A -> B sA

Which of the following gives the size of an array strs?

strs.length

What does Inheritance do?

subclass extends superclass

In the balanced parentheses application, if the next character is a closing parenthesis, then:

then it is compared to the character which is popped off the stack

In the balanced parentheses application, if the next character is an opening parenthesis, then:

then it is pushed on the stack

In the balanced parentheses application, if the next character is a closing parenthesis and the stack is empty, then:

then the expression is not balanced

Given the stack [ what value does the isEmpty operation return?

true

True or false: If a method test() does not have a return value, it should not appear in the following statement. x = 5 * test();

true

Suppose your method does not return any value, which of the following can be used as the return type?

void

Given two reference variables r1 and r2, which of the following statements are true? - If r1.equals(r2) is true, then r1 == r2 is true. - r1.equals(r2) may cause a compile error, because the signature of the method equals may not match the type of r1 and r2. - r1 == r2 is always false, becasue they are rerference variables and refers to different objects. - If r1 == r2 is true, then r1.equals(r2) is true.

If r1 == r2 is true, then r1.equals(r2) is true.

Analyze the following main method: public static void main(String[] args) { int num = (int) (Math.random() * 2); if (num % 2 == 0) { System.out.println("head"); } else { System.out.println("tail"); return; } }

It compiles fine.

What's the value of x and y after the call of swap(x, y) as shown in the following code segment? public static void swap(double x, double y) { double temp = x; x = y; y = temp; } public static void main(String[] args) { double x = 3.0, y = 7.0; swap(x, y); }

x: 3.0 y: 7.0

What is the best code to retrieve the value from the first element and to remove the first element of a linked list for a stack (e.g. as done as part of a pop operation)? Assume the list stores elements of type T.

T firstElem = top.getVal(); top= top.getNext();

What is the correct way to declare an array which can hold 10 elements of some generic type T? T[] array = new T[10](); T[] array = new T[10]; T[] array = (T[])(new Object[9]); T[] array = (T[]) (new Object[10]);

T[] array = (T[]) (new Object[10]);

What's the output of the following program? String[] strs = new String[3]; for (int i = 0; i < strs.length; i++) { System.out.println(strs[i].length()); }

The code will result in a NullPointerException.

What is wrong with the following program? public class Circle { int radius; public Circle (double newRadius) { System.out.println(newRadius); } public static void main(String[] args) { Circle c = null; System.out.println(c.radius); } }

The program has a NullPointerException because c is null while executing c.radius.

Given the stack [G, R, T, S, V what element is removed from the stack by a pop operation?

V

Given the following code segments, public class A { } public class B extends A { } public class Test { public static void main(String[] args) { A a = new A(); B b = new B(); } }

b instanceof A a instanceof A b instanceof B

Which of the following is NOT an attribute(field) of the stack implementation given in the BoundedArrayStack.java file? DEFAULT_CAPACITY stack bottom topIndex

bottom

Which of the following keyword is required when declaring a class?

class

What does encapsulation do?

data hiding

For the following code segment, which access modifier can be used in the front of line 3? 1: package a; 2: public class A { 3: ______ int num; 4: } package a; public class B extends A { public void print() { System.out.println(num); } }

default protected public

True or false: A subclass of an abstract class must override all abstract methods.

false

True or false: All methods in an abstract class must be abstract.

false

True or false: All the parameters in a method must be of the same type.

false

True or false: If a method test() has a return value, it should not appear in the following statement. test();

false

What is the output of the following code? public class A { boolean value; public static void main(String[] args) { A a = new A(); System.out.println(a.value); } }

false

True or false: Every method must have a return statement.

false; void methods don't have a return statement

Iterator interface has the following functions:

hasNext ( ): is there another element to process? next ( ) next ( ): return the element to be processed remove ( ): remove ( ): remove the current element

Iterable iterface has the following functions:

iterator ( )

What is the best code to add a new first element to a linked list for a stack (e.g. as done as part of a push operation)? Assume that the stack is not empty. Assume the new element already exists and is referenced by newTop.

newTop.setNext(top); top = newTop;

What is the output of the following code? int num = 0; while (num < 4) { num = num + 1; System.out.println("num is " + num); }

num is 1 num is 2 num is 3 num is 4

Analyze the following statements, and check all valid Java statements. - public abstract void c(){}; - private abstract void b(); - public abstract void a(); - abstract void c(){};

public abstract void a();

Assume that StackImpl is a correct implementation of the generic StackInterface from the book. Assume that Student is an existing class. Which of the following are correct? - StackInterface <Student> studentStack; - StackInterface <Student> studentStack = new StackImpl <Student>; - StackInterface <String> wordStack; - StackInterface <Integer> intStack; - StackInterface <String> wordStack = new StackInterface <String>(); - StackInterface <Student> studentStack = new StackImpl <Student>(); - StackInterface <Student> studentStack = new StackImpl <Double>(); - StackInterface <int> intStack;

- StackInterface <Student> studentStack; - StackInterface <String> wordStack; - StackInterface <Integer> intStack; - StackInterface <Student> studentStack = new StackImpl <Student>();

The file StackInterface.java uses a generic type T. A stack can be declared as StackInterface<xyz> stack = new Stack<xyz>(); What are valid types which can replace xyz in the declaration? - String - int - double - Integer - Circle(assuming a Circle.java file exists and defines a Circle class)

- String - Integer - Circle(assuming a Circle.java file exists and defines a Circle class)

Which statement about constructor is correct?

- The constructor must have the same name with the class. - A constructor doesn't have a return type.

Given the declaration and intantiation below. Assume that StackImpl is a valid implementation of the generic StackInterface and that the class Circle exists. StackInterface <Circle> circStack = StackImpl <Circle>( ); Which of the following statements compile without error? - circStack.isFull(); - circStack.pop(); - circStack.pop (new Circle()); - circStack.push( new Circle()); - circStack.top(); - circStack.size(); - circStack.push(); - circStack.isEmpty();

- circStack.isFull(); - circStack.pop(); - circStack.push( new Circle()); - circStack.top(); - circStack.isEmpty();

Which of the following are methods which must be implemented for the Iterator interface? - hasNext ( ) - remove ( ) - iterable() - iterator ( ) - size ( ) - next ( )

- hasNext ( ) - remove ( ) - next ( )

Which of the following is not part of the stack interface (that is in StackInterface.java)? - push - isEmpty - size - isFull - pop - top

- size

What's the output of the following program? int[] nums = new int[3]; for (int i = 0; i < nums.length; i++) { System.out.println(nums[i]); }

0 0 0

What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = new int[5]; int i; for (i = 0; i < nums.length; i++) { nums[i] = i; System.out.print(nums[i] + " "); } } }

0 1 2 3 4

Order the four access modifiers from the most restrictive to the least restrictive.

1 - private 2 - default 3 - protected 4 - public

What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6}; for (int i = nums.length - 3; i >= 0; i--) { nums[i + 2] = nums[i]; } for (int i: nums) { System.out.print(i + " "); } } }

1 2 1 2 3 4

What will be the output from the following code segment? int num = 0; while (num < 10) { num = num + 1; } System.out.println(num);

10

What's the output of the following code segment if the following is used as the input from the keyboard? 5 3 6 5 7 8 2 1 9 0 public static void main(String[] args) { Scanner stdin = new Scanner(System.in); double target = stdin.nextDouble(); int cnt = 0; double val = 1; while (val > 0 && cnt < 3) { val = stdin.nextDouble(); if (val > target) { cnt++; } } System.out.print(cnt + " " + val); stdin.close(); }

3 8.0

What's the output of the following program? class Test { public static void main(String[] args) { Circle c = new Circle(); c.radius = 3; System.out.println(c.radius); } } class Circle { double radius; public void doubleRadius(double radius) { radius *= 2; } }

3.0

What's the output of the following program? class Test { public static void main(String[] args) { Circle c = new Circle(); c.radius = 3; doubleRadius(c.radius); System.out.println(c.radius); } public static void doubleRadius(double radius) { radius *= 2; } } class Circle { double radius; }

3.0

Given the stack [M, N, P, Q what value does the size operation return?

4

How many lines of stars will be printed by the following code segment? int num = 0; while (num++ < 5) { System.out.println("*"); }

5

What's the output of the following code? int num = 17; int cnt = 0; while(num != 0) { num = num / 2; cnt++; } System.out.print(cnt);

5

What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = { 2, 4, 6, 6, 5, 6 }; int max = nums[0]; int indexOfMax = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] > max) { max = nums[i]; indexOfMax = i; } } System.out.println(max + "," + indexOfMax); } }

6, 2

What's the output of the following program? class Test { public static void main(String[] args) { Circle c = new Circle(); c.radius = 3; doubleRadius(c); System.out.println(c.radius); } public static void doubleRadius(Circle c) { c.radius *= 2; } } class Circle { double radius; }

6.0

A stack with 8 elements is represented by a linked list. That linked list has ____ non-null next attributes (links to a next element).

7

What's the output of the following code segment? class A { int val;; public A() { val = 5; } public int getVal() { return val; } } class B extends A { public B() { val = 7; } } public class Test { public static void main(String[] args) { B b = new B(); System.out.print(b.getVal()); } }

7

What's the output of the following code segment? class A { public A() { System.out.println("A"); } } class B extends A { public B() { System.out.println("B"); } } class C extends B { public C() { System.out.println("C"); } } public class Test { public static void main(String[] args) { C c = new C(); } }

A B C

_____ defines objects of the same type.

A class

Analyze the following code segment: abstract class A{ A() { System.out.print("A"); } } public class B extends A { B() { System.out.print("B"); } public static void main(String[] args) { A a = new B(); B b = new B(); } }

ABAB

Which is the correct way of declaring and instantiating an ArrayList of Circle objects? - ArrayList<Circle> circLst = new ArrayList<Circle>; - ArrayList <Circle> circLst = new ArrayList<Circle>(); - ArrayList<Circle> circLst = new ArrayList(); - ArrayList circLst = new ArrayList();

ArrayList <Circle> circLst = new ArrayList<Circle>();

For the following code segment public class Child extends Parent { } class Parent { public Parent(int x) { } }

Compile error since there is no default constructor in Parent class.

public class Square { double side; public Square() { this(2); } public Square(double side) { this.side = side; } }

Compiles fine.

Given the stack [A, B, H what element is removed from the stack by a pop operation?

H

What is the output of the following program? public class Test { public static void main(String[] args) { String[] strs = {"Hello", "World"}; for (String str : strs) { System.out.print(str + "#"); } } }

Hello#World#

What is the correct code to declare and create a new object of type LinkedNode to hold an element of type String? Call the reference elemToAdd. - LinkedNode elemToAdd = new LinkedNode(); - LinkedNode<String> elemToAdd = new LinkedNode<String>(); - LinkedNode<String> elemToAdd = new LinkedNode<String>;

LinkedNode<String> elemToAdd = new LinkedNode<String>();

Given the stack [M, N, P what element does the top operation return?

P

What's the output of the following code segment? class Shape { public Shape() { System.out.println("Shape without parameter"); } public Shape(int len) { System.out.println("Shape with one parameter"); } } class Rectangle extends Shape { public Rectangle() { System.out.println("Rectangle without parameter"); } public Rectangle(int length) { System.out.println("Rectangle with one parameter"); } public Rectangle(int length, int width) { super(length); System.out.println("Rectangle with one parameter"); } } class Test { public static void main(String[] args) { new Rectangle(); new Rectangle(10); new Rectangle(10, 10); } }

Shape without parameter Rectangle without parameter Shape without parameter Rectangle with one parameter Shape with one parameter Rectangle with one parameter

Analyze the following code: // defined in a file named Test.java class Circle { double radius; Circle(double newRadius) { radius = newRadius; } void printArea() { System.out.print(radius * radius * 3.14); } } public class Test { public static void main(String[] args) { Circle a = new Circle(); a.printArea(); } }

The program has a compilation error because class Circle does not have an explicit default constructor but has a non-default constructor.

Consider the following program: public class Print5 { public static void main(String[] args) { print5(); } public static void print5() { for (int i = 0; i < 5; i++) System.out.print("*"); } }

The program output five stars.

Consider the following program: public class Print5 { public static void main(String[] args) { // print5(); } public static void print5() { for (int i = 0; i < 5; i++) System.out.print("*"); } }

The program output nothing.

What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = new int[5]; int i; for (i = 0; i < nums.length; i++) { nums[i] = i; } System.out.print(nums[i] + " "); } }

The program will cause ArrayIndexOutOfBoundsException.

Consider the following code, public class Square { double side; public void Square(double side) { this.side = side; } }

There is a compile error because a constructor cannot have a return type.

Select all expressions which are balanced. [ ( ) { } [ ( ] ) [ ( ) { } { [ ( ) ( ) ] ( ) } ] { ( ( ) ) [ ] } [ ( ) ]

[ ( ) { } { [ ( ) ( ) ] ( ) } ] { ( ( ) ) [ ] } [ ( ) ]

Given a stack with holds names as elements. The current stack is [ Susan, Leslie, Pat, Sasha. The element to be pushed is Leslie. What is the stack after the push operation?

[ Susan, Leslie, Pat, Sasha, Leslie

Given the stack [A, B, C show the stack after two pop operations have been performed.

[A

Given the stack [A, B, C show the stack after one pop operation has been performed.

[A, B

Given the stack [A, B, C show the stack after element C is pushed.

[A, B, C, C

Given the stack [A, B, C show the stack after element E is pushed.

[A, B, C, E

Given the stack [ show the stack after element K is pushed.

[K

Given the stack [X, Y, K? show the stack after one top operation has been performed?

[X, Y, K

What does composition do?

a class may have attributes of different types.

What does polymorphism do?

a super class variable can invoke a subclass method.

A line of code in the given BoundedArrayStack.java implementation is this(DEFAULT_CAPACITY) There is no method with the name 'this', which method is called?

public BoundedArrayStack(int capacity)

Which access modifier is used where one wants data members to be accessed by the inherited classes but not from all classes?

protected

Which access modifier may be used to substitute the in the following code segment? interface A { void methodX(); } class B implements A { <access-modifier> void methodX() { System.out.println("In method X"); } }

public

For the following code segment, which access modifier can be used in the front of line 3? 1: package a; 2: public class A { 3: ________ int num; 4: } package b; import a.A; public class B extends A { public void print() { System.out.println(num); } }

public protected


Related study sets

SFST test questions (25 questions)

View Set

chapter 12 personal finance managment final

View Set

Appreciation of the Visual Arts - Test 2

View Set

Nutrition Exam 1 Practice Questions

View Set