Examination Questions Midterm 1

Similar documents
Examination Questions Midterm 2

Section 05: Solutions

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

COS 126 General Computer Science Spring Written Exam 1

Selected Questions from by Nageshwara Rao

SOLUTIONS (INCOMPLETE, UNCHECKED)

COS 126 Midterm 1 Written Exam Fall 2012

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS61B Spring 2016 Guerrilla Section 2 Worksheet

CS111: PROGRAMMING LANGUAGE II

COS 126 Midterm 1 Written Exam Fall 2011

Basic Data Structures

Basic Data Structures 1 / 24

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSC 1351: Quiz 6: Sort and Search

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS171 Midterm Exam. October 29, Name:

CE204 Data Structures and Algorithms Part 2

COS 226 Algorithms and Data Structures Spring Midterm

Final Exam. COMP Summer I June 26, points

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

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

NATIONAL UNIVERSITY OF SINGAPORE

CS4215 Programming Language Implementation

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

Interfaces & Generics

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

Queues. Stacks and Queues

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

Introduction to Computing II (ITI 1121) Final Examination

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Data Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101

CS 314 Midterm 2 Fall 2012

Midterm 1 Written Exam

Midterm Exam 2 CS 455, Spring 2014

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Midterm Exam 2 CS 455, Spring 2013

CS 314 Midterm 2 Spring 2013

COS 226 Algorithms and Data Structures Fall Midterm

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Computer Science 1 Ah

Midterm Exam 2 CS 455, Spring 2011

COMP 250 Midterm #2 March 11 th 2013

CSE 1223: Exam II Autumn 2016

Computer Science 62. Midterm Examination

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Introduction to Computing II (ITI 1121) Final Examination

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

CS 61B Spring 2017 Guerrilla Section 3 Solutions

DNHI Homework 3 Solutions List, Stacs and Queues

Outline. iterator review iterator implementation the Java foreach statement testing

CS 139 Practice Midterm Questions #2

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

University of Maryland College Park Dept of Computer Science

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

Summer Final Exam Review Session August 5, 2009

Lab 5 1 Due Thu., 9 Mar. 2017

Symbol Tables 1 / 15

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

CS 314 Exam 2 Fall 2017

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Topic 10: The Java Collections Framework (and Iterators)

NATIONAL UNIVERSITY OF SINGAPORE

Csci 102: Sample Exam

Full file at

CSE wi: Practice Midterm

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Computer Programming, I. Laboratory Manual. Final Exam Solution

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

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

Midterm Examination (MTA)

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Java Simple Data Types

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Generics. IRS W-9 Form

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

Lesson 26: ArrayList (W08D1)

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

Computational Expression

CS 113 MIDTERM EXAM 2 SPRING 2013

CSCI 1103: Array-based Data Structures

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

Transcription:

CS1102s Data Structures and Algorithms 10/2/2010 Examination Questions Midterm 1 This examination question booklet has 9 pages, including this cover page, and contains 15 questions. You have 40 minutes to complete the exam. Use a 2B pencil to fill up the provided MCQ form. Leave Section A blank. Fill up Sections B and C. 1

Java Question 1: Consider the following Java program: public class Test { public static int i = 0; public static int f ( int j ) { i = j + 1; return i i ; public static void main( String [ ] args ) { int k = f ( 3); System. out. println ( i ) ; What will be the output of the program? A 0 B 4 C 16 D Compilation error: assignment to static variable i is not allowed. E None of the above Answer 1: B The static variable i is assigned to 4 in the function call, and thus 4 is printed. 2

Question 2: The type Integer is a subtype of the type Number. Consider the following Java program fragment: class A { public void f ( Integer x) { System. out. println (x ) ; Number i = 75; new A( ). f ( i ) ; A This program leads to a compilation error because the the function call f( i) does not correspond to a function f in class A that accepts arguments of type Number. B This program leads to a runtime error because the function call f(i) does not find a function f that accepts arguments of type Number. C This program can be compiled with no error, and when executed, prints the number 75. D This program leads to a compilation error because the int 75 is assigned to the Number variable i. E None of the above Answer 2: A The type Number is not a sub-type of Integer, and thus the type of the function f does not include the type of the actual argument of the function call. Question 3: Consider the following Java program fragment: switch (x) {... This fragment may be compiled without error if it is preceeded by the declaration A java. util. Iterator x B Comparable x C boolean x D int x E none of the below 3

Answer 3: D switch statements require expressions of type int. Question 4: Assume a class definition of the following form: class A {... Consider the following program fragment: Object [ ] arr = new A[ 1 0 0 ] ; arr [100] = new Object ( ) ; Which statement on this program fragment is true? A This program fragment can be compiled with no error, and when executed, adds a new Object to the array arr. B This program fragment leads to a compilation error because the variable arr is declared of type Object[], but assigned a value of type A[]. C This program fragment leads to a compilation error, because a variable that refers to an array of A values is used in an array assignment involving a value of type Object. D This program fragment throws an exception, because a variable that refers to an array of A values is used in an array assignment involving a value of type Object. E This program fragment throws an exception, because only indices from 0 to 99 are allowed for an array of size 100. Answer 4: D The co-variance of array types allows the program to be well-typed, but the Java runtime check prevents array assignment, leading to an ArrayStoreException. This is therefore one possible answer. Answer 4: E This is another possible answer; before an array assignment is carried out, the array is checked whether its size is sufficient. This is not the case here, so an IndexOutOfBounds exception could be raised. The Java Language Specification does not make clear, which one of the two runtime checks is carried out first, so both answers are admissible. 4

Program Analysis Question 5: Which statement on the growth of constant functions is true? A 100 = O(0) B 0 = O(100) C 100 = Θ(0) D 0 = Θ(100) E all of the above Answer 5: B Review the definition of O(...) and Θ. Question 6: Consider the following Java program fragment: for ( int i =0; i i < N; i++) for ( int j =0; j j < N; j++) System. out. println ( Hello! ) ; Which one of the following statements about the runtime R(N) is true? A R(N) = Θ(log N) B R(N) = Θ( N) C R(N) = Θ(N) D R(N) = Θ(N 2 ) E R(N) = Θ(2 N ) Answer 6: C Note that the inner loop will be executed N N times. 5

Question 7: Consider the following Java program fragment: for ( int i =1; i < N N N; i++) for ( int j =0; j < N N N; j++) x = x + 1; Which one of the following statements about the runtime R(N) is true? A R(N) = O(N) B R(N) = O(N 3 ) C R(N) = O(N 6 ) D R(N) = O(N 9 ) E None of the above Answer 7: C Question 8: Let F be a function such that F(N) = Θ(N 3 ). Which one of the following statements is true? 8 A F(N) = o(n 2 ) holds 8 B F(N) = o(n 2 ) does not hold Answer 8: 8 B Note the definition of o(...). Question 9: Let F be a function such that F(N) = Θ(N 3 ). Which one of the following statements is true? 9 A F(N) = o(n 3 ) holds 9 B F(N) = o(n 3 ) does not hold 6

Answer 9: 9 B Note the definition of o(...). Question 10: Let F be a function such that F(N) = Θ(N 3 ). Which one of the following statements is true? 10 A F(N) = o(n 4 ) holds 10 B F(N) = o(n 4 ) does not hold Answer 10: 10 A Note the definition of o(...). 7

Lists, Stacks, Queues Question 11: Consider the following Java function: public static int sum( java. u t i l. LinkedList<Integer > mylist ) { int thesum = 0; for ( Integer x : mylist ) thesum += mylist. remove (0); return thesum ; Which one of the following statements is true? A This function is guaranteed to return the sum of all elements of the given mylist. B This function cannot be compiled, because the iteration variable x is not used within the body of the for-each loop. C This function will lead to a runtime error if applied to a non-empty list mylist, because a function argument (the read-only argument mylist) is modified using remove. D The behavior of this function is not well-defined, because the collection mylist is modified during iteration. E None of the above Answer 11: D Modification of the data structure that underlies an iterator is not allowed. Answer 11: E The specification of the Java API makes it clear that an ConcurrentModificationException is raised. In that sense, the behavior is well-defined. The question was vague; we consider both alternatives as correct. 8

Question 12: Consider the following Java program fragment, whose runtime R is a function of the input size N. java. u t i l. ArrayList<Integer > a = new java. u t i l. ArrayList<Integer >(); for ( int i = 0; i < N; i++) a. add (0, i ) ; Which of the following is correct: 1 A R(N) = Θ(N) 1 B R(N) = Θ(N 2 ) 1 C R(N) = Θ(N 3 ) 1 D R(N) = Θ(N 4 ) 1 E R(N) = Θ(N log N) Answer 12: 1 B The operation a.add(0,i) on ArrayList has linear time complexity, wrt to the current size of the data structure. Therefore, overall we have quadratic time complexity. Question 13: Consider the following Java program fragment, whose runtime R is a function of the input size N. java. u t i l. ArrayList<Integer > a = new java. u t i l. ArrayList<Integer >(); for ( int i = 0; i < N; i++) a. add( i, i ) ; for ( int j = 0; j < N; j++) a. remove (N j 1); Which one of the following statements about the runtime R(N) is true? 1 A R(N) = Θ(N) 1 B R(N) = Θ(N 2 ) 1 C R(N) = Θ(N 3 ) 1 D R(N) = Θ(N 4 ) 1 E R(N) = Θ(N log N) 9

Answer 13: 1 A Both loop bodies have constant time complexity because they operate the end of the ArrayList. Question 14: Consider the following Java program fragment, whose runtime R is a function of the input size N. java. u t i l. LinkedList<Integer > k = new java. u t i l. LinkedList<Integer >(); for ( int i = 0; i < N; i++) for ( int j = 0; j < N; j++) k. add(k. size ()/2, j ) ; Which one of the following statements about the runtime R(N) is true? 1 A R(N) = Θ(N) 1 B R(N) = Θ(N 2 ) 1 C R(N) = Θ(N 3 ) 1 D R(N) = Θ(N 4 ) 1 E R(N) = Θ(N log N) Answer 14: 1 D The LinkedList grows to quadratic size during the execution of the program fragment. Thus the body of the inner loop has quadratic complexity. The inner loop itself is executed N 2 times. 10

Question 15: Consider the possibility of implementing a queue using the class java. util.arraylist. Which one of the following statements is false? A If the enqueue operation for item x is done using add(0,x) then the dequeue operation for item y can be done using remove(size() 1). B If the enqueue operation for item x is done using add(size() 1,x) then the dequeue operation for item y can be done using remove(0). C An implementation of queues using java. util.arraylist can be done such that both enqueue and dequeue require O(1) time. D If an item x is entered into the queue before an item y using enqueue, then repeated applications of dequeue will return x before y. E One of the above is false. Answer 15: E Regarding alternative C: We can use the ArrayList as an array, and proceed with the technique described in Section 3.7.2, leading to constant time enqueue and dequeue operations. Answer 15: C In class we mentioned that ArrayList is not preferable for implementing arrays; LinkedList is preferrable because no extra book-keeping is required. Answer 15: B The enqueue operation should be done using add(size(), x) and not using add(size() 1,x). Because of the possible confusion, we allow all three answers. 11