Final Exam COP 3337

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the output of this program? class A { int i; } class B extends A { Int j; void display() { super.i = j + 1; System.out.println(j + " " + i); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }

2 3

What is printed? public class Exceptions { public Exceptions() { try { for (int n = 3; n >= -3; n--) System.out.println(100 / n); } catch (ArithmeticException e) { System.out.println("O-Divide!"); } finally { System.out.println("Done'); } public static void main(String[] args) { new Exceptions(); } }

33 50 100 0-Divide! Done

A class that has at least one abstract method is called an: a. Encapsulated class b. Concrete class c. Private class d. Abstract class

Abstract class

You cannot create an object using a/an: a. Superclass constructor b. Subclass constructor c. Abstract class constructor d. Ancestor class constructor

Abstract class constructor

What does a derived class automatically inherit from the base class? a. Instance variables b. All of the above c. Static variables d. Public methods

All of the above

If an instance variable is not modified by public, protected or private then it is said to have: a. All options are correct b. Default access c. Package access d. Friendly access

All options are correct

Java does not use late binding for methods marked as: a. Static b. All options are correct c. Private d. Final

All options are correct

Each catch clause that follows a try block is called: a. An exception block b. An exception listener c. An error handler d. An exception handler

An exception handler

An object of a derived class has the type of the derived class, and it also has the type of every one of its ________ classes. a. Child b. Ancestor c. Descendent

Ancestor

A super class is also called a a. Dominant class b. Derived class c. Sub class d. Base class

Base class

Order of execution of constructors in Java inheritance is a. Base to derived class b. Derived to base class c. Random order d. None

Base to derived class

___________ refers to the process of associating a method definition with a method invocation a. Encapsulation b. Inheritance c. Binding d. Polymorphism

Binding

A method or instance variable modified by protected: a. Can not be accessed by name inside its own class definitions b. Can not be accessed by name in any other class (that is, other than own class, derived class and same package). c. Can not be accessed by name inside any class derived from it. d. Can not be accessed by name in the definition of any class in the same package.

Can not be accessed by name in any other class (that is, other than own class, derived class and same package).

Exceptions that are subject to the catch or declare rule are called: a. Errors b. Unchecked exceptions c. Fatal exceptions d. Checked exceptions

Checked exceptions

Inheritance is the process by which a new class - known as a _______ - is created from another class, is called the _______.

Derived class, base class

Assigning an object of an ancestor class to a descendant class is called: a. Dynamic binding b. Upcasting c. Static binding d. Downcasting

Downcasting

A method marked as final means the compiler uses _________ binding. a. Early b. No binding c. Late d. Strong binding

Early

_____________ binding refers to the method definition being associated with the method invocation when the code is compiled a. Late b. Dynamic c. None d. Early

Early

What are the three main programming mechanisms that constitute object-oriented programming? a. Polymorphism, recursion, abstraction b. Encapsulation, abstraction, inheritance c. Inheritance, polymorphism, recursion d. Encapsulation, inheritance, polymorphism

Encapsulation, inheritance, polymorphism

What is printed? public class Inherit { public class Figure { public Figure() { System.out.println("Figure"); } } public class Rectangle extends Figure { void display() { System.out.println("Rectangle"); } } public class Box extends Rectangle { void display() { System.out.println("Box"); } } public Inherit() { Figure f = new Figure(); Rectangle r = new Rectangle(); r.display(); Box b = new Box(); b.display(); } public static void main(String[] args) { new Inherit(); } }

Figure Figure Rectangle Figure Box

What is printed? public class Inherit { public class Figure { public Figure() { System.out.println("Figure"); } } public class Rectangle extends Figure { void display() { System.out.println("Rectangle"); } } public class Box extends Rectangle { void display() { System.out.println("Box"); } } public 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(); } }

Figure Rectangle Box

A __________ block executes regardless of whether an exception occurs. a. Final b. Try c. Finally d. Catch

Finally

In using the keyword this in place of super(), the invocation of this must be the _________ action taken by the constructor. a. Last b. First c. It does not matter d. None

First

The keyword extends indicates: a. Inheritance b. Polymorphism c. None of the above d. Encapsulation

Inheritance

When defining your own exception class, you extend an existing exception class. This is an example of: a. Encapsulation b. Abstraction c. Inheritance d. Polymorphism

Inheritance

When using the Scanner class one should account for a/an: a. ArrayIndexOutOfBoundsException b. NumberFormatException c. InputMismatchException d. PowerFailureException

InputMismatchException

What can you say about this code? public class Speaker { public void speak() { System.out.println("Woof, woof!"); } int age = 3; } public class Inherit { public class Dog extends Speaker { protected String name; } public class MyDog extends Dog { MyDog() {name = "Lassie": } public void speak() { System.out.println("My name is " + name); System.out.println("My age is " + age); } } public static void main(Stringil args) { new MyDog().speak(): } }

It prints two lines: My name is Lassie My age is 3

What is printed? public class Inherit { public abstract class Figure { void display() { System.out.println("Figure"); } } public class Line extends Figure { public void display() { System.out.println("Line"); } } public void tryme(Figure f) { f.display(); } public Inherit() { Figure f = new Line(); tryme(f); } public static void main(String[] args) { Inherit i=new Inherit(); } }

Line

What is printed? public class Inherit { public abstract class Speaker { public abstract void speak(); } public class Cat extends Speaker public void speak() { System.out.println("Woof!"); } } public class Dog extends Speaker { public void speak() { System.out.println("Meow!"); } } public Inherit() { Speaker d = new Dog(); Speaker c = new Cat(); d.speak(); C.speak(); } public static void main(String[] args) { new Inherit(); } }

Meow! Woof!

What is printed? public class MyException extends Exception { public MyException() { super(); System.out.println("MyException"); } public MyException(String s) { super(s); System.out.println("MyException: + s); System.exit(0); } } public class Except { public static void main(String[] args) { new MyException("Testing"); new MyException(); } }

MyException: Testing

The catch block has _________ parameters. a. Two b. One c. Three d. Zero

One

What is printed? public class MyException extends Exception { public MyException() throws MyException { System.out.println("one"); throw new MyException("three"); } public MyException(String s) { System.out.println("two:" + s); } } public class Except { public Except() { try { throw new MyException(); } catch (MyException e) { System.out.println("MyException:+ e); } catch (Exception e) { System.out.println("Exception:" + e); } finally { System.out.println("finally"); } } public static void main(String[] args) throws MyException { new Except(): System.out.println("done"); } }

One Two: three MyException: MyException Finally Done

Which statement(s) is (are) true? a. Java upcasts automatically, but you must explicitly downcast b. Java downcasts automatically, but you must explicitly upcast c. Java expects the user to explicitly upcast and downcast d. Java will both upcast and downcast automatically e. The rules for upcasting and downcasting depend upon whether classes are declared public, protected, or private

Only statement (a) is true

Which of the following statements is (are) true? a. If a class is declared to be abstract then every method in the class is abstract and must be overridden b. If a class is declared to be abstract then some methods in the class may have their bodies omitted c. If a class is declared to be abstract then all methods in the class must have their bodies omitted d. If a class is declared to be abstract then all the instance variables must be overridden when a concrete class is derived from the abstract base class

Only statement (b) is true

Which statements are true? a. Overriding is just another term for overloading b. Overloading only can be done within an inheritance structure c. Overriding only can be done within an inheritance structure d. You can overload a constructor e. You can override a constructor

Only statement (c) and (d) are true

What statement(s) are true? a) Static variables and methods always are inherited, even if they're marked private b) Static variables and methods only are inherited if no public, protected, or private modifier is used for them c) Static variables and methods only are inherited if they're not declared private d) Static variables and methods follow different rules regarding their visibility in inheritance situations

Only statement (c) is true

A base class is synonymous with a: a. Derived class b. Parent class c. Child class d. Sub class

Parent class

_____________ refers to the ability to associate many meanings to one method name by means of the late binding mechanism a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Polymorphism

"public class Aggregate" is incorrect. Choose the correct line so that this program prints: Granite: weight=25.0 value=4 numKind=7 public class Inherit { public abstract class Stone protected float weight = 13; protected int value = 4; abstract public String toString(); } public class Aggregate { protected int numKind; } public class Granite extends Aggregate { Granite() { weight = 25; numKind = 7: } public String toString() { return "Granite: weight=" + weight + " value=" + value + " numKind=" + numKind; } } public Inherit() { Granite g = new Granite(); System.out.println(g); } public static void main(Stringl] args) { Inherit I = new Inherit(); } }

Public abstract class Aggregate extends Stone {

What is printed? public class Inherit { public abstract class Figure { public void display() { System.out.println("Figure"); } } public abstract class Rectangle extends Figure { } public class Box extends Rectangle { public void display() { System.out.println("Rectangle"); } } public Inherit() { Figure f = (Figure) new Box(); f.display(); Rectangle r = (Rectangle) f; r.display(); } public static void main(String[] args) { Inherit i=new Inherit(); } }

Rectangle Rectangle

Inheritance promotes code _________. a. Reuse b. Repeats c. Reinvention d. All of the above

Reuse

A derived class is also called a a. Base class b. Super class c. Sub class d. All of the above

Sub class

If a method throws an exception, and the exception is not caught inside the method, then the method invocation: a. Transfers control to the catch block b. None is true c. Transfers control to the exception handler d. Terminates

Terminates

If the final modifier is added to the definition of a method, this means: a. The method may be redefined in the derived class b. The method may not be redefined in the derived class c. The method may be redefined in the sub class. d. None

The method may not be redefined in the derived class

Try blocks contain code that could possibly: a. Handle an exception b. Catch an exception c. Throw an exception d. Display an exception

Throw an exception

The execution of a throw statement is referred to as: a. Handling an exception b. Catching a block c. Throwing an exception d. Trying a block

Throwing an exception

If a method does not throw an exception, then it must at least warn programmers that any invocation of the method might possibly throw an exception. This warning is called a/an: a. Exception handler b. Try block c. Catch clause d. Throws clause

Throws clause

Which statement(s) are correct? a. If a method may throw an exception, it must either catch it or use a throws clause b. If a method may throw an exception, it must catch it c. A method may include a throws clause for an exception that it does not throw d. A method may not include a throws clause unless it actually throws an uncaught exception

True statements: (a) and (c)

Which of the following are true? a. If an exception is thrown within a try block, the finally clause always executes. b. If no exception is thrown within a try block, the finally clause always executes. c. If an exception is thrown within an exception handler, the finally clause always executes. d. If an exception is thrown within a finally block, the exception is propagated out of the finally block, and the finally block does not complete its execution.

True: a, b, c, d.

Assigning an object of a derive class to a variable of a base class is called: a. Upcasting b. Static binding c. Dynamic binding d. Downcasting

Upcasting

Say you want to access a variable or method in a base class, from a derived class, but it is marked private. Which statement is true? a. You'd have to create an accessor or mutator in order to access the variable (or method) - there is no other way to access it b. No problem. Just use super to access it from the derived class c. No problem. Since the derived class inherits base class variables and methods, just us the name of the variable or method. d. No problem. Just mark it static and it will become visible.

You'd have to create an accessor or mutator in order to access the variable (or method) - there is no other way to access it

Which of the following is true about the interfaces in java. 1. An interface can contain following type of members. ...public, static, final fields (i.e., constants) ...default and static methods with bodies 2. An instance of interface can be created. 3. A class can implement multiple interfaces 4. Many classes can implement the same interface

a) 1,3 and 4

A class can implement more than one interface? a) True b) False

a) True

will this compile? interface A{int n = 111; } class B implements A{ void inc() { n++; } }

a) True

Match the definitions with their suitable terms a) The last item that is added is the first item that is removed (LIFO) b) The first item that is added is the first term that is removed (FIFO) c) Can use a circular array for its implementation d) An ordered set of data elements, each containing a link to its successor (and sometimes its predecessor) e) _______, increases the list by 100% when another element is added if the list is full f) It is an inner class without a name and for which only a single object is created

a) stack b) queue c) Queue d) Linked List e) Vector List f) Anonymous Class

What does the following function print for n=25? public static void fun(int n) { if (n==0) return; System.out.print(n%2); fun(n/2);

b) 10011

Consider the following recursive function fun(x,y). What is the value of fun(4,3) public static int fun(int x, int y) { if (x==0) return y; return y; return fun(x-1, x+y); }

b) 13

Predict the output of the following code public class Quiz4 {public static int fun(int n) { if (n==4)else return 2*fun(n+1); } public static void main(String[] args) { System.out.println(fun(2));} }

b) 16

Is this code written correctly? class A{String s = "AAA"; void methodA() { System.out.println(s); } static class B { void methodB() } } { } } }

b) False

public class A{ public static void main(String[] args) { Stack<Integer> stackOne = new Stack<Integer>(); Stack<Integer> stackTwo = new Stack<Integer>(); } } for(int i =0; i<5; i++) stackOne.push(i); stackTwo.push(stackOne.pop()); stackTwo.push(stackOne.pop()); stackTwo.push(stackOne.pop()); What are the values returned if the following code is added at the end of the program? stackOne.peek(); stackTwo.peek();

b) No answer text provided

When a class implements an interface, it need to ____ all the interfaces methods? a) Overload b) Override

b) Override

Which of the following is true about anonymous inner classes? a. You can create 'n' number of objects to anonymous inner classes b. Anonymous inner classes will not have the name c. You can instantiate anonymous inner classes only once

c) Both B and c are true

What will this code print? public class A{public static void main(String[] args) { } } Queue<Integer> orders= new LinkedList<Integer>(); orders.add("Orange Juice"); orders.add("Mangoes"); orders.add("Almond Milk"); orders.add("Eggs"); orders.add("Blueberries"); String a = orders.poll(); String b = orders.peek(); String c = orders.poll(); String d = orders.peek(); System.out.println(a+ ", " + b + ", "+ c + ", " + d);

c) There is a compile time error in this code

which statement is correct? public interface L { public void go(); } class M {public void go() { //Line 1 if (x==0) return y; return fun(x - 1, x + y); 12) What does the following function print for n=25? public static void fun(int n) { if (n==0) return; System.out.print(n%2); fun(n/2); //Line 2 public void aMethod(); //Line 3 System.out.println("GO!"); //Line 4 } } class N extends M implements L{} public class A{public static void main(String[] args) { N a = new N(); a.go(); } }

c) this will print GO!

Which of the following points is/are true about linked list data structure when it is compared with array a. It is easy to insert and delete elements in linked list b. Random access is not allowed in a typical implementation of Linked Lists c. The size of array has to be pre-decided, linked lists can change their size any time d. All are true

d) All are true

Which of the following can not be a member of an interface? a) Non Static Data Members b) Static Methods c) Constructors d) All in the answer list

d) All in the answer list

What is the value of x? public class Test {LinkedList<Integer> list= new LinkedList<Integer>(); list.add(1); Integer x =list.get(1); } }

d) An exception is generated

Which statement is true about a static nested class? a) It must extend the enclosing class b) You must have a reference to an instance of the enclosing class in order to instantiate it c) Its variables and methods must be static d) It does not have access to nonstatic members of the enclosing class

d) It does not have access to nonstatic members of the enclosing class

Which line will cause an error public interface A { final int k=8; private int m; String fun(char s); }

d) Line 2

What is the ArrayList nums if it is initially [5,3,1] and the following code is executed? Nums.add(6); Nums.add(0,4); Nums.remove(1);

d) [4,3,1,6]

Which statement is true? a. You can use the abstract modifier on both variables and methods b. An abstract method can be declared private, so long as you provide a body for it c. An abstract method can be declared protected so long as you also declare it to be static d. An abstract method cannot be declared to be private

d. An abstract method cannot be declared to be private

Given the following code, what is the result? public class Quiz4 {static class Char { //line 1 char c;public Char(char c) { this.c = c; public void print() { System.out.print(c); } } private static void typeText(Char c) { c.print(); } public static void main(String[] args) { typeText(new Char('A')); typeText(new Char('B'){ a) compile error as class N does not override go method b) the program compiles without error and prints nothing c) this will print GO! d) Runtime error as class N does not override go method } public void print() { System.out.print("["); super.print(); System.out.print("]"); }

e) A[B]

What is printed? public class Inherit { public class Animal { private int numEyes = 1; private int numFeet = 2; public Animal() {} public Animal(int numEyes) { this.numEyes = numEyes; } public Animal(int numEyes, int numFeet) { this(numEyes); this.numFeet = numFeet; } public String toString() { return "numEyes=" + numEyes + "numFeet=" + numFeet; } } public static void main(String[] args) { Animal a1 = new Animal(); Animal a 2 = new Animal(5); Animal a3 = new Animal(4, 6); System.out.println(a1); System.out.println(a2); System.out.println(a3); } }

numEyes=1 numFeet=2 numEyes=5 numFeet=2 numEyes=4 numFeet=6

The special syntax for invoking a constructor of the base class is: a. super() b. child() c. base() d. parent()

super()


Conjuntos de estudio relacionados

Chapter 8 Intercultural Communication

View Set

HW2 - Background Material, cont.

View Set

Chapter 1 Test Vocab (fill in the blank)

View Set