University of Massachusetts Amherst, Electrical and Computer Engineering

Similar documents
CS 101 Fall 2005 Midterm 2 Name: ID:

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

CMP 326 Midterm Fall 2015

Selenium Class 9 - Java Operators

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

University of Palestine. Mid Exam Total Grade: 100

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Problem Grade Total

CSE 11 Midterm Fall 2008

Java Review. Fundamentals of Computer Science

Last Name: Circle One: OCW Non-OCW

JAVA OPERATORS GENERAL

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

CS 101 Spring 2007 Midterm 2 Name: ID:

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Midterm Exam 2 CS 455, Spring 2011

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

CS 113 MIDTERM EXAM 2 SPRING 2013

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Comp Intermediate Programming EXAM #1 February 16, 2004 Rice University - Instructors: Cox & Nguyen

CSE 113 A. Announcements - Lab

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

CS171 Midterm Exam. October 29, Name:

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

CS171:Introduction to Computer Science II

CS 116 Week 8 Page 1

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

Java Control Statements

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CIS 162 Project 4 Farkle (a dice game)

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Final Exam. COMP Summer I June 26, points

I. True/False: (2 points each)

Assignment 1 due Monday at 11:59pm

CMSC 331 Second Midterm Exam

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Question: Total Points: Score:

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

CS171:Introduction to Computer Science II

5) (4 points) What is the value of the boolean variable equals after the following statement?

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CS 101 Spring 2006 Final Exam Name: ID:

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

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

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

Introduction to Computer Science II (CSI 1101)

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Prof. Navrati Saxena TA: Rochak Sachan

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Question: Total Points: Score:

Name CIS 201 Midterm II: Chapters 1-8

CS 101 Exam 2 Spring Id Name

Java Programming for Selenium

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Task 1: Print a series of random integers between 0 and 99 until the value 77 is printed. Use the Random class to generate random numbers.

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

CS 10, Fall 2015, Professor Prasad Jayanti

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Question: Total Points: Score:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CS56 Midterm Exam. E01, W16, Phill Conrad, UC Santa Barbara Wednesday, 02/10/2015

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

CS 101 Fall 2006 Midterm 3 Name: ID:

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

Introduction to Programming Using Java (98-388)

Introduction to the Java Basics: Control Flow Statements

CS100M November 30, 2000

Programming Basics. Digital Urban Visualization. People as Flows. ia

Practice Midterm 1. Problem Points Score TOTAL 50

Repetition, Looping. While Loop

Java Outline (Upto Exam 2)

CSC Java Programming, Fall Java Data Types and Control Constructs

COMP 401 Spring 2014 Midterm 1

1st Semester Examinations CITS1001 3

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

Computer Sciences 302 Exam 2 Information & Sample Exam

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

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

MIDTERM REVIEW. midterminformation.htm

Transcription:

University of Massachusetts Amherst, Electrical and Computer Engineering ECE 122 Midterm Exam 1 Makeup Answer key March 2, 2018 Instructions: Closed book, Calculators allowed; Duration:120 minutes; Write your name and Student ID in all pages (in provided fields); Write your answer in the provided space and be sure to show all your work clearly. Notice there are 20 extra points in this exam, but the maximum grade is 100. Name: Lab Session (check box): Tu 10:00AM 11:15AM Tu 1:00PM - 2:15PM Th 10:00AM 11:15AM Th 1:00PM - 2:15PM Problem 1: (20 points) Problem 2: (20 points) Problem 3: (20 points) Problem 4: (20 points) Problem 5: (20 points) Problem 6: (20 points) Total: (100 points) 1. [20] What is the following code doing? Hint: try running the code in your head with different values of a and b and see what would be the result. public void p1method(int a, int b) int c=1; while (b>0) c*=a; b--; System.out.println(c); The code above is a method that receives two integers a and b and prints a b (a to the power of b).

2. [20] Convert the method presented below to use a while loop instead of a for loop. You should write the entire method in your answer. Hint: Which variable is your iterator or loop control variable? public int p2method(int a) int c=0; for (int i=0; i<a; i++) c+=i; return c; public int p2method(int a) int c=0; int i=0; while (i<a) c+=i; i++; return c;

3. [20 pts] In mathematics, the harmonic series is the divergent infinite series: 1 i=1 i =1+ 1 2 + 1 3 + 1 4 + 1 5 +... Write a Java method that calculates and returns the sum of the first n terms of the harmonic series: 1 i =1+1 2 + 1 3 + 1 4 + 1 5 +...+ 1 n 1 +1 n n i=1 Your method should receive n as an input parameter and return the result as double. public static double p3method(int n) double sum=0; for (int i = 1; i<=n ; i++) sum+=1.0/i; return sum;

4. [20] What is printed out on the console when you call the following function with values of 2 and 7 for a and b, respectively? public static void p4method(int a, int b) System.out.println(a/b); System.out.println((float)a/b); int i; for (i = 0 ; i<b ; i++) if (i%3 == 0) continue; System.out.println(i); System.out.println(i); switch (a) case 1: System.out.println(a+b); break; case 2: System.out.println(a-b); default: System.out.println(0); if (a>b) System.out.println(1); System.out.println(a>0); 0 0.4 1 2 4 5-3 0 true

5. [20] Given the StandardDie Class description below, which implements a Standard Die (6 faces), write a Java method that receives an integer parameter n, instantiates 2 dice and rolls both dice n times. In each iteration if face values on dice are equal, you should print Bonus!. import java.util.random; public class StandardDie private int value; private Random rand; public Die(int f) rand = new Random(); public int getvalue() return value; public int roll() value=rand.nextint(6)+1; return value; public static void p5method(int n) StandardDie d1 = new StandardDie(); StandardDie d2 = new StandardDie(); for (int i=0; i<n; i++) if (d1.roll() == d2.roll()) System.out.println("Bonus!");

6. [20 pts] Write Java code for Cat class according to the following specification: Class variables (all private) - name (String) - Cat name - feral (boolean) - Is it a feral cat or not? - scratchedlasttime (boolean) - did the cat scratch you last time play was called? Methods (all public) - Cat (constructor) receives the cat name and feral property and attributes those values to cat properties. Variable scratchedlasttime should be initially false. - getname getter method for name. - play Must return a string representing the what happens when you try to play with the cat. Feral cats will always scratch you (return ouch ). Cats that are not feral will not scratch you (return nice kitty ) if they didn t scratch last time play was called, otherwise this should just return ouch. Hint: use scratchedlasttime for that purpose. public class Cat private String name; private boolean feral; private boolean scratchedlasttime; public Cat(String n, boolean f) name = n; feral = f; scratchedlasttime = false; public String getname() return name; public String play() if (feral) return "ouch"; else if (scratchedlasttime) scratchedlasttime = false; return "nice kitty"; else scratchedlasttime = true; return "ouch";