7
Name: Intro to Computer Science NetID: Midterm Exam #2, Fall 2013 Page 1 Fill-in-the-blank (2% each) 1. _______________________ are a useful data structure for storing and manipulating grid-like tabular data in code. 2. The System.out.printf() method is used to _______________________ 3. An object is an _______________________ of a class. 4. The “new” operator in Java is used to _______________________ 5. Dot-notation (using the period “.” in code) is used to indicate _______________________ 6. Fill in two blanks to make this class inherit from the Mammal class and explicitly call it’s constructor when instantiated. public class Dog ____________ Mammal { String name; Dog() { ____________ //call the parent class’s constructor System.out.println(“Whoof!”); //let us know that it’s alive } } 7. Getters and setters are used to get the value or set the value of an _______________________ variable or attribute. 8. The _______________________ keyword is used to indicate that a method or attribute belongs to the class, and not to any particular object. 9. _______________________ is the term used to indicate that a user of a class should be able to treat the class like a black box. 10. _______________________ of variables and methods within an object or class is one of the techniques by which the answer to #9 is usually achieved by making those variables and methods “belong to” a particular object or class.

Java Midterm Questions

Embed Size (px)

DESCRIPTION

Sample practice questions for the Java Midterm.

Citation preview

  • Name: Intro to Computer Science

    NetID: Midterm Exam #2, Fall 2013

    ! Page 1

    Fill-in-the-blank (2% each)

    1. _______________________ are a useful data structure for storing and manipulating grid-like

    tabular data in code.

    2. The System.out.printf() method is used to _______________________

    3. An object is an _______________________ of a class.

    4. The new operator in Java is used to _______________________

    5. Dot-notation (using the period . in code) is used to indicate _______________________

    6. Fill in two blanks to make this class inherit from the Mammal class and explicitly call its constructor when instantiated. public'class'Dog'____________ Mammal'{'''''String'name;'''''Dog()'{'''''''''____________ //call'the'parent'classs'constructor'''''''''System.out.println(Whoof!);'//let'us'know'that'its'alive'''''}'}'

    7. Getters and setters are used to get the value or set the value of an

    _______________________ variable or attribute.

    8. The _______________________ keyword is used to indicate that a method or attribute

    belongs to the class, and not to any particular object.

    9. _______________________ is the term used to indicate that a user of a class should be able

    to treat the class like a black box.

    10. _______________________ of variables and methods within an object or class is one of the

    techniques by which the answer to #9 is usually achieved by making those variables and

    methods belong to a particular object or class.

  • Name: Intro to Computer Science

    NetID: Midterm Exam #2, Fall 2013

    ! Page 2

    Multiple choice (2% each)

    1. Each element in a two-dimensional array must have the same number of sub-elements.

    a. True b. False

    2. All programs in Java are object-oriented as a requirement of the language.

    a. True b. False

    3. Constructors in a class definition (select all that apply):

    a. must accept no parameters b. must have no return value c. must be overloaded at least once d. must not be declared as public or private

    4. The keyword super is a reference to a classs (select all that apply):

    a. no-args constructor b. parent class c. instantiated object d. package e. access modifier

    5. To adhere to the concept of abstraction, where possible, data fields in a Java program

    should be made. a. public b. static c. private d. void e. -None of the above-

  • Name: Intro to Computer Science

    NetID: Midterm Exam #2, Fall 2013

    ! Page 3

    6. Taken from a larger class definition, the following method accepts as its parameter

    public static void changeAPairOfGlasses(Glasses thisPairOfGlasses) { thisPairOfGlasses.dirtify(); }

    a. A Glasses object, passed by value b. A Glasses object, passed by reference c. A static Glasses class d. null e. None of the above-

    7. In the following code, what does the variable x point to? (assume the variable x has been initialized properly in code not displayed)

    x'='new'Integer(10);'

    a. a primitive b. an object c. a class d. a constructor e. -All of the above-

    8. The == operator performs a comparison.

    a. by reference b. by value

    9. All Processing programs inherit from PApplet, which has draw() and setup() methods. When you build your own custom Processing sketch, you create your own draw() and setup() methods. These methods (select all that apply) '

    a. overload the PApplet classs methods b. override the PApplet classs methods c. are constructors for the PApplet class d. are setter methods e. are instance attributes

    10. The special keyword, this, when used within a static method of a class, refers to

    a. the current class b. the current object c. the parent object d. All of the above- e. None of the above-

  • Name: Intro to Computer Science

    NetID: Midterm Exam #2, Fall 2013

    ! Page 4

    Algorithms (20% each)

    1. Write a program that uses a two-dimensional array to store 5 students names and

    midterm #2 grades, including yourself as one of those students. Include a method that accepts as its sole parameter the name of a student, and that outputs that students name and grade. Format the output of this method nicely such that the name occupies 15 characters of the output, and the grade occupies 4 characters.

  • Name: Intro to Computer Science

    NetID: Midterm Exam #2, Fall 2013

    ! Page 5

    2. Write a class that has the attributes and methods represented in the following UML diagram. Getter and setter methods for all private properties have not been included in the UML diagram, but must be included in the code. Also include two constructors: one that can accepts parameters to set all a ChocolateBar objects properties, and one that can be used with parameters for only the isDark and percentCocoaSolids properties, and which gives default values to the other properties. Create a main method that creates two of these objects one using each constructor. Call each ChocolateBars melt() method.

    ChocolateBar

    -isOrganic : boolean

    -isFairTrade : boolean

    -isDark : boolean

    +percentCocoaSolids : int

    -sourceCountry : String

    +melt() : void

  • Name: Intro to Computer Science

    NetID: Midterm Exam #2, Fall 2013

    ! Page 6

    3. Create a ComputerScienceCourse class and a Student class, using the following outlines. Anything not included or specified in the outline left up to your discretion.

    ComputerScienceCourse Student title : String name : String description : String email : String days : String array (a String for each day, such as M, T, W, R, F, etc)

    year : int (use 1 for freshman, 2 for sophomore, 3 for junior, 4 for senior)

    time : String (such as 9:30-10:45) grade: String

    students : Student array (an array of 50 Student objects) assignGrade(grade : String)

    registerStudent(student : Student) : void

    a. Include a main() method that creates a ComputerScienceCourse object representing this course.

    b. Create a Student object representing yourself, and one other Student object representing someone else.

    c. Register both Student objects for this course by calling the registerStudent() method and passing it the Student objects.

    d. The registerStudent() method should add these two Students to the first two spots in the ComputerScienceCourse objects students array.

    e. Assign the Student objects whatever grade you think you deserve by calling the Student objects assignGrade() method. This should update the Student objects grade property.

  • Name: Intro to Computer Science

    NetID: Midterm Exam #2, Fall 2013

    ! Page 7

    Extra credit (10% extra)

    1. Create a Processing program that shows a circle that oscillates back and forth in the horizontal x direction. The following instance methods and attributes available to all programs that inherit from the PApplet class might help (assume all attributes are of the float data type): void setup() automatically called once to set up the window void draw() automatically called continuously to draw each frame of the animation void size(width, height) creates a window void background(r,g,b) fills the window with a solid background color. void ellipse(x, y, width, height) creates an ellipse at the specified position with the

    specified dimensions width holds with width of the window height holds the height of the window