CMSC132 Fall 2021 Exam #1: Object-Oriented Programming Concepts and Java Fundamentals - Pr, Exams of Object Oriented Programming

This exam covers fundamental concepts in object-oriented programming (oop) and their implementation in java. It includes questions on data abstraction, procedural abstraction, inheritance, interfaces, static binding, dynamic binding, and exception handling. The exam also features exercises that require students to implement classes and methods, demonstrating their understanding of oop principles and java syntax.

Typology: Exams

2020/2021

Uploaded on 04/07/2025

patrick-ng-1
patrick-ng-1 🇺🇸

3 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
University of Maryland College Park
Department of Computer Science
CMSC132 Fall 2021
Exam #1
FIRSTNAME, LASTNAME (PRINT IN UPPERCASE):
STUDENT ID (e.g. 123456789):
Instructions
Please print your answers and use a pencil.
This exam is a closed-book, closed-notes exam with a duration of 50 minutes and 200 total points.
Do not remove the exam’s staple. Removing it will interfere with the scanning process (even if you staple the exam again).
Write your directory id (e.g., terps1, not UID) at the bottom of pages with DirectoryId.
Provide answers in the rectangular areas.
Do not remove any exam pages. Even if you don’t use the extra pages for scratch work, return them with the rest of the exam.
Your code must be efficient and as short as possible.
If you continue a problem on the extra page(s) provided, make a note on the particular problem.
For multiple choice questions you can assume only one answer is expected, unless stated otherwise.
You don’t need to use meaningful variable names; however, we expect good indentation.
You must write your name and id at this point (we will not wait for you after time is up).
You must stop writing once time is up.
Grader Use Only
#1
Problem #1 (Miscellaneous)
40
#2
Problem #2 (Class Implementation)
60
Total
Total
100
pf3
pf4
pf5

Partial preview of the text

Download CMSC132 Fall 2021 Exam #1: Object-Oriented Programming Concepts and Java Fundamentals - Pr and more Exams Object Oriented Programming in PDF only on Docsity!

University of Maryland College Park

Department of Computer Science

CMSC132 Fall 2021

Exam

FIRSTNAME, LASTNAME (PRINT IN UPPERCASE):

STUDENT ID (e.g. 123456789):

Instructions

  • Please print your answers and use a pencil.
  • This exam is a closed-book, closed-notes exam with a duration of 50 minutes and 200 total points.
  • Do not remove the exam’s staple. Removing it will interfere with the scanning process (even if you staple the exam again).
  • Write your directory id (e.g., terps1, not UID) at the bottom of pages with DirectoryId.
  • Provide answers in the rectangular areas.
  • Do not remove any exam pages. Even if you don’t use the extra pages for scratch work, return them with the rest of the exam.
  • Your code must be efficient and as short as possible.
  • If you continue a problem on the extra page(s) provided, make a note on the particular problem.
  • For multiple choice questions you can assume only one answer is expected, unless stated otherwise.
  • You don’t need to use meaningful variable names; however, we expect good indentation.
  • You must write your name and id at this point (we will not wait for you after time is up).
  • You must stop writing once time is up.

Grader Use Only

#1 Problem #1 (Miscellaneous) 40 #2 Problem #2 (Class Implementation) 60 Total Total 100

Problem #1 (Miscellaneous)

  1. (3 pts) Calling Collections.sort() in your code would be an example of ______ a. using data abstraction b. using procedural abstraction c. invoking an abstract method of an interface that has been implemented d. None of the above Answer: b
  2. (3 pts) Assume a csStudent class implements a Person Interface and extends a Student class. Which is not true? a. csStudent “is a” Student b. csStudent “is a” Person c. A csStudent object can be assigned to a variable of type Person d. Student “is a” csStudent Answer: d
  3. ( 3 pts) Which statement is false. a. You can have an abstract class without any abstract methods. b. If a class is missing a default constructor (i.e. one that takes not argument), it cannot be a base class. c. If a class is declared as final it can inherit from a non-final base class. d. An Enum type can have a private constructor. Answer: b
  4. ( 3 pts) In no more than 2 sentences, explain what is meant by static (early) binding.
  5. (3 pts) Assume a csStudent class implements a Person Interface and extends a Student class. What will take place when the following code fragment is executed? Object o = new Student(); boolean b = o instanceof csStudent;

a. An exception will be generated.

b. false will be assigned to b.

c. true will be assigned to b

d. It will not even compile.

Answer: b

  1. (3 pts) What Java feature is most prominently being demonstrated here? ArrayList myList = new ArrayList (); myList.add(5);

a. autoboxing

b. unboxing

c. inheritance

d. interfaces

Answer: a With static binding, the declared type of a variable is used to determine what methods are called. With dynamic binding, the method that is called depends on an object’s underlying type (the type of the actual object instantiated that is assigned to a variable, no matter the type of that variable).

Problem #2 (Class Implementation) Assume the following 2 classes that you should not change. public abstract class Gadget { private int id; public Gadget(int id) { this.id = id;} @Override public String toString() { return "Gadget [id=" + id + "]";} public abstract String howToUse(); } public class PortableTimeGadget extends TimeGadget { public PortableTimeGadget(int id, int hour, int min) { super(id, hour, min); } } For this problem you will complete the implementation of the TimeGadget , TimeComparator , and Util classes (whose partial definitions are provided below). A TimeGadget object has a hour and min field. The other 2 do not have fields. All 5 classes are in the same package. Y ou may not add any instance nor static variables and you may not add any auxiliary methods to the classes. public class TimeGadget extends Gadget{ private int hour; private int min; public int getHour() { return hour; } public int getMin() { return min; } / INCOMPLETE CLASS / } import java.util.Comparator; public class TimeComparator implements Comparator { / INCOMPLETE CLASS / } import java.util.ArrayList; public class Util { / INCOMPLETE CLASS / } Below you will see a sample driver and expected output that illustrates the functionality of the classes you need to implement.

Sample Driver / Output

import java.util.ArrayList; import java.util.Arrays; public class SampleDriver { public static void main(String[] args) { TimeGadget t1 = new TimeGadget(35, 7, 17); TimeGadget t2 = new TimeGadget(36, 7, 18); TimeGadget t3 = new TimeGadget(37, 7, 17); TimeComparator timeComparator = new TimeComparator(); int [] result = {timeComparator.compare(t1, t2),timeComparator.compare(t1, t3), timeComparator.compare(t2, t3)}; System.out.println(Arrays.toString(result)); Gadget t4 = new TimeGadget(38, 12, 3); Gadget t5 = new PortableTimeGadget(39, 2, 45); ArrayList g = new ArrayList (); g.add(t1); g.add(t2); g.add(t3); g.add(t4); g.add(t5); Util.demo(); System.out.println(Util.makeList(g));} } [-1, 0, 1] Bad Time [Gadget [id=35] 7:17, Gadget [id=36] 7:18, Gadget [id=37] 7:17, Gadget [id=38] 12:03]

  1. TimeGadget Class Methods (30 pts)

a. Constructor (14 pts) - It has as parameters the id, hour and min. It will call the base class constructor to set the id. If the hour

is outside of the range of 1 to 12 (inclusive) or the min is outside of the range 0 to 59 (inclusive) it will throw the IllegalArgumentException with the message Bad Time, otherwise it will initialize the corresponding instance variables.

Solution:

public TimeGadget(int id, int hour, int min) { super(id); if(hour < 1 || hour > 12 || min < 0 || min > 59) { throw new IllegalArgumentException("Bad Time"); } this.hour = hour; this.min = min; } b. getTime (10 pts) – The public non-static getTime method will return a String and take in no parameters. The return string will be the hour followed by a : followed by the min. If the min (not the hour) is less than 10, it should be padded with a zero. Therefore, 7:03 is valid but 7:3 is not when hour is 7 and min is 3.

Solution:

public String getTime() { return hour + ":" + (min < 10? " 0 " : "") + min; } c. howToUse (6 pts) – The public non-static howToUse method will return a String and take in no parameters. The return string will be simply be Set time and look at it. You must have the Override annotation before the method definition.

Solution:

@Override public String howToUse() { return "Set time and look at it"; }

  1. TimeComparator Class Method (10 pts) a. compare - The public non-static compare method has parameters TimeGadget t1, TimeGadget t2 and will return 1 if t1 is greater than t2, 0 if they are equal, and - 1 if t1 is less than t2. As for ordering based on time, 1:00 is the smallest time and 12:59 is the largest.

Potential Solution:

public int compare(TimeGadget t1, TimeGadget t2) { if(t1.getHour() == t2.getHour() && t1.getMin() == t2.getMin()) { return 0; } else if(t1.getHour() > t2.getHour() || (t1.getHour() == t2.getHour() && t1.getMin() > t2.getMin())) { return 1; } return - 1 ; }