ClassCastException - Data Structures - Exams, Exams of Data Structures and Algorithms

Main points of this exam paper are: Classcastexception, Theprogram Stops, Public Intlist, Intlist Tail, Whose Tail, Static Intlist, Java Program, Classcastexception, Source File, New Source

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

72 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 61B
Midterm #1
Fall 2001
Professor Paul Hilfinger
Throughout this test, assume that the following definitions are available.
class IntList {
public int head;
public IntList tail;
/** The IntList whose head is HEAD and whose tail is TAIL. */
public IntList (int head, IntList tail) { ... }
/** Return the reverse of L (non-destructively). */
static IntList reverse (IntList L) { ... }
}
Problem #1: [5 points]
a. Your Java program keeps throwing a ClassCastException on a line of your program that reads
v = (String) values.pop ();
where values is a java.util.Stack. As a first step in figuring out what's going on, you want to find out what
value is about to be popped at this point. Tell what commands you would give gjdb to find out.
b. I add a new source file, mymath/Vector.java, to my mcalc project, but don't change my Makefile.
Usually, comilation (with gmake) continues to work, but occasionally, something goes wrong. What, and
under what conditions?
c. Another student shows you this fragment of program text:
{
List L = createList();
fillValues(L);
if (L.contains (x)) {...
TheProgram stops with a NullPointerException at the if statement, so the student knows that L must be null.
He tells you that he guesses this is caused by a problem with
CS 61B, Midterm #1, Fall 2001
1

Partial preview of the text

Download ClassCastException - Data Structures - Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 61B

Midterm

Fall 2001

Professor Paul Hilfinger

Throughout this test, assume that the following definitions are available.

class IntList { public int head; public IntList tail;

/** The IntList whose head is HEAD and whose tail is TAIL. */ public IntList (int head, IntList tail) { ... }

/** Return the reverse of L (non-destructively). */ static IntList reverse (IntList L) { ... } }

Problem #1: [5 points]

a. Your Java program keeps throwing a ClassCastException on a line of your program that reads

v = (String) values.pop ();

where values is a java.util.Stack. As a first step in figuring out what's going on, you want to find out what value is about to be popped at this point. Tell what commands you would give gjdb to find out.

b. I add a new source file, mymath/Vector.java, to my mcalc project, but don't change my Makefile. Usually, comilation (with gmake) continues to work, but occasionally, something goes wrong. What, and under what conditions?

c. Another student shows you this fragment of program text:

List L = createList(); fillValues(L); if (L.contains (x)) {...

TheProgram stops with a NullPointerException at the if statement, so the student knows that L must be null. He tells you that he guesses this is caused by a problem with

CS 61B, Midterm #1, Fall 2001