Java Quiz for CMSC 433 - Fall 2003, Quizzes of Programming Languages

A Java quiz for the CMSC 433 class during the Fall 2003 semester. The quiz consists of multiple-choice questions designed to test students' knowledge of Java features, including objects, dynamic dispatch, subtyping, and exceptions. The quiz includes class definitions, code sequences, and expected answers.

Typology: Quizzes

2019/2020

Uploaded on 11/25/2020

koofers-user-7ag-1
koofers-user-7ag-1 🇺🇸

5

(1)

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Quiz
CMSC 433
Programming Language Technologies and Paradigms
Fall 2003
September 2, 2003
This quiz (7 pages, both sides of the page) is designed to test your knowledge of basic, but tricky
Java features. Don’t get worried about how you do on the quiz—this is for fun, and to get you thinking
about Java again. However, if you do well, the quiz score can benefit your final grade.
We will use the results of the quiz to understand how well everyone understands Java in the class.
Thursday’s lecture will cover material that appears on this quiz.
Question Points Score
1 15
2 30
3 30
4 25
Total 100
pf3
pf4
pf5

Partial preview of the text

Download Java Quiz for CMSC 433 - Fall 2003 and more Quizzes Programming Languages in PDF only on Docsity!

Java Quiz

CMSC 433

Programming Language Technologies and Paradigms

Fall 2003

September 2, 2003

This quiz (7 pages, both sides of the page) is designed to test your knowledge of basic, but tricky Java features. Don’t get worried about how you do on the quiz—this is for fun, and to get you thinking about Java again. However, if you do well, the quiz score can benefit your final grade. We will use the results of the quiz to understand how well everyone understands Java in the class. Thursday’s lecture will cover material that appears on this quiz.

Question Points Score

Total 100

  1. (Objects, 15 points)

class MyInt { int x; public MyInt(int x) { this.x = x; } } class Foo { void inc(int x) { x = x+1; } void inc(MyInt o) { o.x = o.x+1; } }

Given the above class definitions, what is the output of the following code sequence:

MyInt o = new MyInt(5); int x = 5; Foo f = new Foo();

f.inc(x); f.inc(o); System.out.println(x); System.out.println(o.x);

Answer:

5 6

true true false true true false true false false true true true true true

  1. (Subtyping, 30 points)

Foo

FooIfc

BarIfc

Bar

BifIfc

Bif Legend

extends

implements

Interface

Class

Given the above class and interface relationships, what will be the output of the following code sequence? Recall that o instanceof C returns true when the actual run-time type of o is a subtype of C. If any of the instanceof checks fail to compile, indicate which ones.

Foo f = new Foo(); System.out.println(f instanceof Foo); System.out.println(f instanceof FooIfc);

Bar b = new Bar(); System.out.println(b instanceof FooIfc); System.out.println(b instanceof BarIfc); System.out.println(b instanceof Object);

Object o = b; System.out.println(o instanceof Foo); System.out.println(o instanceof Bar);

FooIfc f2 = new Bar(); System.out.println(f2 instanceof Foo); System.out.println(f2 instanceof FooIfc);

Bif b2 = new Bif(); System.out.println(b2 instanceof FooIfc); System.out.println(b2 instanceof Bar); System.out.println(b2 instanceof BarIfc); System.out.println(b2 instanceof BifIfc);

Answer:

The statement System.out.println(b2 instanceof Bar) does not compile; if removed the output would be: true true true true true true true true true

  1. (Exceptions, 25 points)

class AppException extends Exception { } class AppFault extends AppException { } class AppError extends Error { }

(a) Which of the following methods will cause errors during compilation: class Foo { void f1() { throw new Exception(); } void f2() { throw new AppException(); } void f3() { throw new AppError(); } void f4() throws AppException { throw new Exception(); } void f5() throws AppException { throw new AppFault(); } }

Answer: Methods f1, f2, and f4 fail: they do not declare thrown exceptions.

(b) What will be the output of executing the main method of the Foo class defined next: class Foo { void f1() throws Exception { throw new AppFault(); } void f2() { throw new AppError(); }

public static void main(String args[]) { Foo f = new Foo(); try { f.f1() } catch (AppFault e) { System.out.println("Got AppFault"); } catch (Exception e) { System.out.println("Exception"); } finally { System.out.println("Done"); }

try { f.f2() } catch (Exception e) { System.out.println("Exception"); } finally { System.out.println("Done"); } } }

Answer: Got AppFault Done Done program then exits due to an uncaught AppError