CMSC132 Summer 2018 Midterm 1. Solution

Similar documents
CMSC132 Summer 2018 Midterm 1

Objects and Iterators

CS171 Midterm Exam. October 29, Name:

Introduction to Programming Using Java (98-388)

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

CMSC 331 Second Midterm Exam

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

JAVA MOCK TEST JAVA MOCK TEST II

Subtype Polymorphism

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CMSC 132: Object-Oriented Programming II. Linked Lists

Java Comparable interface

Interfaces. An interface forms a contract between the object and the outside world.

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

Another IS-A Relationship

COMP 401 Spring 2014 Midterm 1

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Final Exam. COMP Summer I June 26, points

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

For this section, we will implement a class with only non-static features, that represents a rectangle

CS 307 Midterm 2 Spring 2011

COMP 401 Spring 2013 Midterm 1

Java Fundamentals (II)

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Interfaces (1/2) An interface forms a contract between the object and the outside world.

CS 314 Exam 2 Fall 2017

CS 113 MIDTERM EXAM 2 SPRING 2013

UMBC CMSC 331 Final Exam

Largest Online Community of VU Students

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Selected Questions from by Nageshwara Rao

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

CLASS DESIGN. Objectives MODULE 4

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Problem Points Score Grader Total 100

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Data Structure. Recitation III

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Examination Questions Midterm 1

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

AP CS Unit 8: Inheritance Exercises

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

1 Shyam sir JAVA Notes

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

CMSC 132, Object-Oriented Programming II Summer Lecture 5:

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CS-202 Introduction to Object Oriented Programming

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

Final Exam CS 251, Intermediate Programming December 13, 2017

Practice Midterm 1. Problem Points Score TOTAL 50

CS 307 Midterm 2 Fall 2010

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Practice Midterm 1 Answer Key

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

COP 3330 Final Exam Review

FORM 2 (Please put your name and form # on the scantron!!!!)

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Lab5. Wooseok Kim

Polymorphism CSCI 201 Principles of Software Development

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

CS/ENGRD 2110 SPRING 2018

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

CS 101 Fall 2005 Midterm 2 Name: ID:

AP CS Unit 6: Inheritance Exercises

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

University of Massachusetts Amherst, Electrical and Computer Engineering

CSE wi Midterm Exam 2/8/18. Name UW ID #

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Super-Classes and sub-classes

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Transcription:

CMSC132 Summer 2018 Midterm 1 Solution First Name (PRINT): Last Name (PRINT): Instructions This exam is a closed-book and closed-notes exam. Total point value is 100 points. The exam is a 80 minutes exam. Please use a pencil to complete the exam. WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0 credit). 1. T/F / 15 2. Multiple Choice / 20 3. Short Answer / 25 4. Programming / 40 Total /100 1

True/False [15 pts] [1 point each] T / F T / F T / F T / F T / F If the linked list is sorted, we can use the binary search algorithm to find a key. A class in Java can have multiple parents You can instantiate interfaces and abstract classes If a class implements the Comparable interface, it is guaranteed to have a compareto() method static blocks are executed before constructors If obj1 is a reference copy of obj2, then obj1 == obj2 When an instance field is not given an access modifier, it is private by default You can always downcast a parent class reference to a child class Overridden methods must call the parent method with super as the first line Inner classes can access the private fields of outer classes Abstract class can define both abstract methods and non-abstract methods T / F T / F An abstract class can have both abstract and non-abstract child classes. An abstract method can be defined in a non-abstract class. A child class can extend a parent or implement an interface, but not do both. An interface can extend another interface 2

Fill in the Blanks and Multiple Choice [20 pts] 1.[2 pts] means the state of an object cannot be changed while means that it can. A. Immutability, mutability B. Static, instance C. Rigidity, flexibility D. None of the above 2. [2 pts] The final keyword can be used to (Circle all that apply): A. To prevent the extension of the class B. To prevent method overriding C. To define symbolic constants D. To prevent method overloading 3. [1 pts] Overriding an instance variable is called, because it makes the parent instance variables of the parent class inaccessible A. Encapsulation B. Polymorphism C. Shadowing D. Autoboxing 4. [2 pts] A circular linked list is one in which the last node is connected to the first node. If we set a node curr equal to first node at the start of traversal, the condition to check if we have reached the end of the list then becomes A. curr == first B. curr.next ==null C. curr.next.next == first D. curr.next == first 5. [3 pts] Suppose Book is an interface and classes Dictionary and Encyclopedia both implement Book. Circle whether each statement is valid or invalid. Book b = new Book(); Valid Invalid Book d = new Dictionary(); Valid Invalid Encyclopedia e = new Book(); Valid Invalid 3

6. [2 pts] What is the output of the following code? class Base { final public void show() { System.out.println("Base::show() called"); class Derived extends Base { public void show() { System.out.println("Derived::show() called"); class Main { public static void main(string[] args) { Base b = new Derived();; b.show(); A. Base::show() called B. Derived::show() called C. Compiler Error D. Runtime Error 7. [2 pts] Assume we compare Rectangles by their areas, implement compareto method for the rectangle class. Class Rectangle implements Comparable<Rectangle>{ Private int width, height;... Public int compareto(rectangle r){ return (width * height) - (r.width * r.height); 4

8. [2 pts] What is the output of the following code? class Base { public static void show() { System.out.println("Base::show() called"); class Derived extends Base { public static void show() { System.out.println("Derived::show() called"); class Main { public static void main(string[] args) { Base b = new Derived();; b.show(); A. Base::show() called B. Derived::show() called C. Compiler Error D. Runtime Error 9.[2 pts] Given print method and a linked list void print(node n) { while (n!= null) { System.out.print(n.data +, ); n = n.next; What will the output be of the code: head = head.next.next; print(head); A. 22, 33, 44, B. 33, 44, 55, C. 44, 55, 66, D. 22, 33, 44,55,66 5

10. [2 pts] What is the output of the following code? class Base { public void Print() { System.out.println("Base"); class Derived extends Base { public void Print() { System.out.println("Derived"); class Main{ public static void DoPrint( Base o ) { o.print(); public static void main(string[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); DoPrint(x); DoPrint(y); DoPrint(z); A.Base Derived Derived C. Base Derived Base B. Base Base Derived D. Compiler Error 6

Short Answer [25 pts] 1. [3 pts] Following code is the content of C.java. Does it compile correctly? Explain why? interface A{ default String m1(){return "A"; interface B{ default String m1(){return "B"; public class C implements A,B{ public String m1(){return "C"; Yes it compiles. There is no issue with java not knowing which m1 to call between A and B because both methods are overwritten in class C. 2. [ 3 pts] Following code is the content of C.java. Does it compile correctly? Explain why? interface A{ String m1(); interface B{ String m1(); public class C implements A,B{ public String m1(){return "C"; Yes it compiles. Because neither A nor B provided a default implementation, class C must provide an implementation which is the one used when m1() is called. 7

3. [3 pts] Following code is the content of C.java. Does it compile correctly? Explain why? interface A{ public String m1(){ public class C implements A{ public String m1(){return "C"; No it does not compile. Because m1 is not given the default keyword, it should not have a method body. There is also a missing semicolon. 4. [3 pts] Does the following code compile correctly? Why? Public class A{ private static int n; public int get(){ return n; Yes it compiles. Instance methods are able to access static class variables. 5. [3 pts] What is the difference between abstract classes and interfaces? Abstract classes can have constructors, Interfaces cannot. Abstract classes are extended, Interfaces are implemented. A class can only extend one abstract class, but can implement multiple interfaces. etc. 8

6. [3 pts] What is the output of the following code? public class A{ static{ System.out.print("A"); { System.out.print("B"); public static void main(string[] s){ new A(); new A(); ABB 7. [3 pts] What is the output of the following code? public class A{ static{ System.out.print("A"); public A(){ System.out.print("B"); public static void main(string[] s){ new A(); new A(); ABB 9

8. [2 pts] What is the output of the following code? class A{ private void m1(){system.out.println("a"); public void m2(){m1(); public class B extends A{ private void m1(){system.out.println("b"); public static void main(string[] s){ A a = new B(); a.m2(); A 9. [2 pts] Can we apply the binary search algorithm on the unsorted array? Explain Why or why not. No, because binary search requires us to look for the midpoint of the array and then search half based on whether the element we are looking for is contained in the larger or smaller half. If the array is unsorted, we won t know to look in the larger or smaller half of the array. 10

Programming [40 pts] 1.[10 pts] In the array based bag class, we increased the capacity when the array is full. User removed some items from the bag, and now we want to shrink the array. Write a method private void shrink(), which shrinks the array by half when the number of items is less than or equal to ¼ of the capacity. For examples: If N = 2, capacity = 8, it means the items array is too big and we have to shrink the array. The shrink method will create an array of size 4, and move the content of the items array to the new array. public class Bag<E>{ private int capacity; private int N; //number of items private E[] items; // other methods are here private void shrink(){ //your code here if (N <= capacity / 4) { E[] smaller = (E[])new Object[capacity / 2]; for (int i = 0; i < N; i++) { smaller[i] = items[i]; items = smaller; capacity /= 2; 11

2. [10 pts] Given a Node class class Node{ Integer data; Node next; and a Node reference head references the first node in the list, write a method int sum(node head) that calculates the sum of all data in a given linked list. Return 0 if the list is empty. For example: sum(head) will return 10 int sum(node head){ //your code here int total = 0; for (Node current = head; current!= null; current = current.next) { total += current.data; return total; 12

3. [10 pts] A Date class has three private members year, month, and day. We want to make the Date class iterable, so that we can iterate through the three values in year, month, and day order. Make the Data class Iterable by filling the blanks. import java.util.iterator; import java.lang.*; public class Date implements Iterable<Integer>{ private int year,month,day; public Date(int y,int m,int d){ year = y; month = m; day = d; @Override public Iterator<Integer> iterator(){ return new DateIterator() ; private class DateIterator implements Iterator<Integer>{ int[] arr = {year, month, day; int count = 0; @Override public boolean hasnext() { return (count < 3); @Override public Integer next() { if (hasnext()) { return arr[count++]; public static void main(string[] s){ Date date = new Date(2018,6,20); for(integer d: date){ System.out.print(d + ","); // Output: 2018,6,20 13

4. [10 pts] Given the sorted array of integers, we want to find an item using the binary search algorithm. Write a method boolean binarysearch(integer[ ] items, Integer item) that checks if the item exists in the items array using the binary search algorithm. boolan binarysearch(integer[] items, Integer item){ int start = 0, end = items.length - 1, mid; while (start <= end) { mid = (start + end) / 2; if (items[mid] == item) { return true; else if (items[mid] > item) { end = mid - 1; else { start = mid + 1; return false; 14