CS 18000 Study Guide - Midterm Guide: Scantron Corporation, Instance Variable, Pariah Dog

38 views7 pages
CS180 Spring 2010 Exam 1 Solutions, 15 February, 2010
Prof. Chris Clifton
Turn Off Your Cell Phone. Use of any electronic device during the test is prohibited.
Time will be tight. If you spend more than the recommended time on any question, go on to the next
one. If you can’t answer it in the recommended time, you are either going in to too much detail or the
question is material you don’t know well. You can skip one or two parts and still demonstrate what I believe
to be an A-level understanding of the material.
Scoring descriptions given in the short answer section are just a rough guide.
Multiple Choice
Provide answers on the Scantron card. (You may mark them on this sheet as well, but the mark on the
Scantron card is the official version.)
1 Method Signatures and Overloading (3 minutes, 2 points)
Given the following class:
public class BaseClass
{
double add(int a, int b) { return a + b; }
int add(int a, int b) { return a + b; } // I.
double add(double a, double b) { return a + b; } // II.
float add(int a, int b) { return a + b; } // III.
int add(int a, int b, int c) { return a + b + c; } // IV.
int add(double a, double b, double c) { return (int)(a + b + c); } // V.
}
Not all of the methods are acceptable - some are proper overloading of the add routine, but others are not
compatible and will result in an error. Which of I., II., III., IV., and V. must be removed for the above
class to compile and be usable.
A III, V
B IV, V
CI, III
D I, II, III
E I, II, III, IV
The signature of a method is based on the name and the number and type of the parameters (but not the type
of the result). Two methods of the same name, but with different number or type of parameters, are different
methods (overloaded).
2 Inheritance (2 minutes, 2 points)
What is the output of the following code:
1
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in
public class BaseClass
{
public void TheMethod() { System.out.println("In base class"); }
}
public class ChildClass extends BaseClass
{
public void TheMethod() { System.out.println("In child class"); }
}
public class Driver
{
public static void main(String[] args)
{
BaseClass c = new ChildClass();
c.TheMethod();
}
}
A In base class
BIn child class
C In base class
In child class
D In child class
In base class
E “BaseClass c = new ChildClass();” will cause a compile error because the type is different.
The (non-static) method in the child class overrides the one in the parent class; any use of that instance uses
the method from the child class.
3 Inheritance/interfaces (3 minutes, 1 points)
Which of the following is true:
A A child class can access all public, protected and private variables and methods of the base class.
B Let “Queue” be an interface, “Queue q = new Queue();” creates an instance of the Queue interface.
CThe “super” keyword is used to access variables and methods of the base class.
D The “this” keyword can be used in a static method.
EA static method cannot access an instance variable.
A static method can be called without an instance being created. In such case, accessing the instance variable
wouldn’t make sense.
2
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Cs180 spring 2010 exam 1 solutions, 15 february, 2010. Use of any electronic device during the test is prohibited. If you spend more than the recommended time on any question, go on to the next one. If you can"t answer it in the recommended time, you are either going in to too much detail or the question is material you don"t know well. You can skip one or two parts and still demonstrate what i believe to be an a-level understanding of the material. Scoring descriptions given in the short answer section are just a rough guide. Provide answers on the scantron card. (you may mark them on this sheet as well, but the mark on the. 1 method signatures and overloading (3 minutes, 2 points) Not all of the methods are acceptable - some are proper overloading of the add routine, but others are not compatible and will result in an error.