COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

Similar documents
COE 212 Engineering Programming. Welcome to Exam I Thursday June 21, Instructor: Dr. Wissam F. Fawaz

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

COE 212 Engineering Programming. Welcome to Exam II Friday April 20, Instructors: Dr. Dima El-khalil Dr. Wissam F. Fawaz

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

COE 212 Engineering Programming. Welcome to Exam II Friday November 27, 2015

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

COE 212 Engineering Programming. Welcome to Exam II Wednesday November 30, 2016

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

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

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

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

Full file at

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Term 1 Unit 1 Week 1 Worksheet: Output Solution

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

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

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

Problem Grade Total

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Practice Midterm 1 Answer Key

Chapter 5 Lab Methods

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Using Classes and Objects. Chapter

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.

CSC 1051 Data Structures and Algorithms I

1 Short Answer (15 Points Each)

Question: Total Points: Score:

Programming with Java

Oct Decision Structures cont d

I. True/False: (2 points each)

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Java Simple Data Types

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

AP CS Unit 3: Control Structures Notes

Chapter 3. Selections

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

JAVA OPERATORS GENERAL

Introduction to Computer Science Unit 2. Exercises

SENIOR HIGH MATH LEAGUE April 24, GROUP IV Emphasis on TRIGONOMETRY

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

Final Exam Practice. Partial credit will be awarded.

Practice Midterm 1. Problem Points Score TOTAL 50

CS Computers & Programming I Review_01 Dr. H. Assadipour

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

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

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

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

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

CS212 Midterm. 1. Read the following code fragments and answer the questions.

AP COMPUTER SCIENCE A

Place your name tag here

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

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

AP Computer Science Unit 1. Programs

Java Simple Data Types

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Formatting Output & Enumerated Types & Wrapper Classes

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

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

Question: Total Points: Score:

H212 Introduction to Software Systems Honors

NATIONAL UNIVERSITY OF SINGAPORE

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

Key Java Simple Data Types

Claremont McKenna College Computer Science

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

CS 113 PRACTICE FINAL

Midterm Examination (MTA)

Question: Total Points: Score:

Chapter 5 Lab Methods

Mr. Monroe s Guide to Mastering Java Syntax

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

COMP 202 Java in one week

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Selection Statements and operators

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

CSC 1051 Data Structures and Algorithms I

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Chapter 2. Elementary Programming

Transcription:

1 COE 211 Computer Programming Welcome to Exam I Tuesday March 13, 2018 Instructors: Dr. Dima El-khalil Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book. Please do not forget to write your name and ID on the first page. 2. You have exactly 105 minutes to complete the 5 required problems. 3. Read each problem carefully. If something appears ambiguous, please write your assumptions. 4. Do not get bogged-down on any one problem, you will have to work fast to complete this exam. 5. Put your answers in the space provided only. No other spaces will be graded or even looked at. Good Luck!!

2 Problem 1: Multiple choice questions (20 minutes) [14 points] For the questions given below, consider the following driver and helper classes: import java.text.decimalformat; public class Geometry { public static void main(string[] args) { Circle c1 = new Circle(); Circle c2 = new Circle(); System.out.println(c1); c2.setarea(4.578); System.out.println(c2.getPerim()); DecimalFormat fmt = new DecimalFormat("0.##"); String area = fmt.format(c2.getarea()); area += 9; System.out.println( fmt.format(double.parsedouble(area))); System.out.println(fmt.format(c1.getArea() +c2.getarea())); public class Circle { private double radius, area, perim; public Circle() { radius = 2; area=0; perim=0; public double getradius(){return radius; public void setarea(double a){area = a; public double getarea() { return area; public double getperim() { return perim; public void calculateperim() { double perimeter = 3.14*2*radius; perim = perimeter; public String tostring() { return Double.toString(getRadius()); 1) What output does the first println statement of the driver class produce? a. 2 b. 0 c. 3.14 2) What output does the second println statement of the driver class produce? a. 2 b. 0 c. 3.14 3) What output does the third println statement of the driver class produce? a. 4.60 b. 4.58 c. 4.59 4) What output does the fourth println statement of the driver class produce? a. 4.60 b. 4.58 c. 4.59 5) How many local variables are created inside the main method of the driver class? a. 4 b. 3 c. 2 6) How many constructors does the driver class have? a. 0 b. 1 c. 2 7) How many local variables are used inside the helper class? a. 0

b. 1 c. 2 8) Consider the body of the getperim method defined inside the helper class. Which of the following can be used to store a more accurate value of the perimeter in the perimeter variable? Note that the perimeter of a circle is 2 a. 2 Math.PI * R b. 2 Math.PI() * R c. (11/5) * Math.PI * R 9) Consider the c1 object created in the driver class. Which of the following statements correctly finds its area and stores it in c1 s area instance variable? Note that the area of a circle is a. c1.setarea(c1.calculateperim()*c1.getradius()/2); b. c1.setarea(math.pi*math.pow(c1.getradius(), 2)); 10) Consider the c2 object created in the driver class. Which of the following statements correctly finds the square root of the area of c2 and then stores it in a String variable called str? a. String str=math.sqrt(c1.getarea())+ ; b. String str=integer.tostring(math.sqrt(c2.getarea())); 11) Assuming that the str variable from the previous question has been created correctly, which of the following can be used to extract the decimal point (.) that it may contain? a. char point=str.charat(str.indexof(. )-1); b. String point=str.substring(str.indexof(. ),str.indexof(. )+1); 12) Which of the following statements correctly creates a variable called diameter that stores the diameter of c1? a. double diameter=2*double.parsedouble(c1.tostring()); b. double diameter=2*c1.radius; 13) Consider the diameter variable from the previous question, which of the following statements correctly prints out the lowest integer whole number that is greater than diameter? For example, if diameter=23.5, then the output should be 24. a. System.out.print((int) diameter); b. System.out.print((int) (diameter+1)); 14) Which of the following correctly prints out the perimeter of c1? a. c1.calculateperim(); System.out.print(c1.getPerim()); b. System.out.print(c1.calculatePerim()); 3

4 Problem 2: True or false questions (10 minutes) [11 points] 1. The following Java statement results in a compile time error: System.out.println( \\\ ); 2. Not providing a constructor for a class results in a compile-time error. 3. The following code fragment results in a run-time. Assume that the DecimalFormat class was imported properly. DecimalFormat fmt = new DecimalFormat( 0.# ); double value = 0.46; String str = fmt.format(value); System.out.println(str.charAt(3)); 4. The following statement prints out to the screen: 4 System.out.print(Math.pow(Math.sqrt(4), 2)); 5. The following statement prints out a random int value between 1 (inclusive) and 5 (inclusive). Assume that rnd is a Random object that was instantiated properly. System.out.print(Math.abs(rnd.nextInt())%5+1); 6. The following statement modifies the value of the String variable called str by replacing every occurrence of the letter a with the letter e. str.replace( a, e ); 7. The following code fragment correctly prints out the last 2 characters of the String variable called str System.out.print( str.charat(str.length()-2)+str.charat(str.length()-1)); 8. The following assignment statement that stores an int literal value in a Double object called obj is syntactically valid; that is, it does not produce a compile-time error. Double obj = 23; 9. All the methods that do not return any value must use void as their return type. 10. The private methods of a class are not allowed to access its instance variables. 11. The following Java statements increment the int variable called val by 3. val += 2; val+ +;

5 Problem 3: Code analysis (15 minutes) [10 points] 1) Consider the helper class given below, along with a driver class for it. public class ClassA { private String str; private int index; public ClassA(String s) { str=s; index=-1; public void first(){ index=str.indexof( : ); public int second() { String str1, str2; str1=str.substring(0,index-1); str2=str.substring(index+2); return third(str1,str2); public int third(string s1,string s2){ return s1.length()+s2.length(); public class ClassADriver { public static void main(string[] args) { String name1= Michael:Jordan ; String name2= Shaquille:Oneal ; ClassA obj1 = new ClassA(name1); ClassA obj2 = new ClassA(name2); obj1.first(); obj2.first(); int val1 = obj1.second(); int val2 = obj2.second(); System.out.print( Answer: + val1+val2); When running the ClassADriver class, what output is produced? a. Answer: 1112 b. Answer: 25 c. An error occurs during the execution of the code d. It doesn t compile correctly e. None of the above 2) Consider the class given below, along with a driver class for it. public class ClassB { private Integer obj1, obj2; public ClassB(int val1, int val2) { obj1 = val1; obj2 = val2; public double first() { return obj1.intvalue()%obj2.intvalue(); public double second() { return obj1.doublevalue()/obj2.intvalue(); public String tostring() { return Double.toString(first() + second()); When running ClassBDriver class, what output is produced? a. Answer: 5.7 b. Answer: 4.5 c. An error occurs during the execution of the code d. It doesn t compile correctly e. None of the above public class ClassBDriver { public static void main(string[] args) { Double obj1 = 7.4, obj2 = 2.8; ClassB obj = new ClassB(obj1.intValue(), obj2.intvalue()); String str = obj + ""; System.out.println("Answer: " + str);

6 Problem 4: Evaluating Java expressions (20 minutes) [25 points] For each of the following code fragments, what is the value of x after the statements are executed? (1) String str = "aaab"; int index1 = (int) Math.random()*2+1; int index2 = (int) (Math.random()*3); char letter1=str.charat(index1); char letter2=str.charat(index2); String x = + letter1 + letter2; Answer: x= aa (2) double val1 = Math.floor(3.5); double val2 = Math.ceil(4.7); double x = Math.pow(val1, (int) val1/(int) val2); Answer: x= 1.0 (3) String nbs = 589 ; int z = Integer.parseInt(nbs.substring(0,2)); int y = Integer.parseInt(nbs.substring(2)) ; int x = ++z ; x -= (z+y) ; Answer: x= -9 (4) DecimalFormat fmt = new DecimalFormat( 00.## ); String str = fmt.format(2.597); String x = str.substring(0, str.length()/2); Answer: x= 02 (5) String str1 = Java is fun ; String str2 = str1.touppercase().tolowercase(); boolean x = str1.equals(str2); Answer: x= false (6) double angle = (int) Math.PI/4; double x = Math.cos(angle); Answer: x= 1.0 (7) String str = Dont worry, be happy ; char x = str.replace( o, a ).charat((str.length()-1)/3); Answer: x= a

7 (8) String str1 = Just do it ; String str2 = str1.replace( u, o ).replace( o, u ); boolean x = str2.equalsignorecase(str1); Answer: x= false (9) String str = Live free or die ; int index1 = str.indexof( e ); int index2 = str.indexof( r ); index1--; index2++; String x = str.substring(index1, index2); Answer: x= ve fr (10) double value = Math.pow(4, 1/2); double x = Math.sqrt(value*value); Answer: x= 1.0 (11) boolean flag = false; String str = flag + ; char x = str.touppercase().charat(str.length()/2); Answer: x= L (12) String S1 = new String("****////"); String S2 = S1.length() + 2 + "2" ; boolean x = S2.equals("102"); Answer: x= true

8 Problem 5: Coding Problems (40 minutes) [40 points] 1. Write a Java program called InsertSpace that reads from the user a String S and an int value representing a valid index within S. Your program should then create a modified version of S called S1 by inserting into S a tab character at the user-supplied index value. After creating S1, your program is required to print it out to the screen. Sample run: Please enter a String: COE211-Computer Programming Please enter the index of tab: 3 The modified String: COE 211-Computer Programming import java.util.scanner; public class InsertSpace { public static void main (String[] args) { Scanner scan= new Scanner(System.in); System.out.print("Please enter a String: "); String S=scan.nextLine(); System.out.print("Please enter the index of tab: "); int a=scan.nextint(); String S1=S.substring(0,a)+"\t"+S.substring(a); System.out.print("The new String: "+S1);

9 2. Consider the right triangle given in the figure below. Write a Java program that reads from the user the length of its hypotenuse c. Your program should then generate a random degree value for one of its acute angles, namely θ (see the figure below). Your program should then use the randomly generated value θ to derive and print out to the screen the lengths of the sides a and b rounded to 4 decimal places. Note that a and b are related to c as follows: b = c sin(θ) and a = c cos(θ). Sample run: Please enter the length of the hypotenuse: 10 Acute angle value: 20 a = 9.3969 b = 3.4202 import java.util.random; import java.util.scanner; import java.text.decimalformat; public class RightTriangle { public static void main(string[] args) { Scanner scan= new Scanner(System.in); System.out.print("Please enter the length of the hypotenuse: "); double H=scan.nextDouble(); Random rnd = new Random(); double angle=(rnd.nextfloat()*90); double S1= H*Math.cos(angle*Math.PI/180); double S2= H*Math.sin(angle*Math.PI/180); DecimalFormat fmt= new DecimalFormat("0.####"); System.out.println("Acute angle value: + angle); System.out.println("a = "+fmt.format(s1)); System.out.println("b = "+fmt.format(s2));

10 3. For a particular car, repair and maintenance costs in year 1 are estimated at x $ and in year 10 at y $, with x and y being two int values. Assuming that the repair cost increases by the same amount every year, write a Java application that asks the user to enter the value of x, the value of y, and a year number. Then, your program should compute and display on the screen the repair and maintenance costs for that year rounded to two decimal places. Sample run: Enter repair and maintenance cost for year 1 in $: 100 Enter repair and maintenance cost for year 10 in $: 1500 Which year would you like to know the cost for? 5 The cost for year 5 is $722.22. import java.text.numberformat; import java.util.scanner; public class TestClass { public static void main(string[] args){ NumberFormat fmt = NumberFormat.getCurrencyInstance(); int x, y, yearnb; double costincrement, totalcost; Scanner scan = new Scanner(System.in); System.out.print("Enter repair and maintenance cost for year 1 in $:"); x = scan.nextint(); System.out.print("Enter repair and maintenance cost for year 10 in $:"); y = scan.nextint(); System.out.print("Which year would you like to know the cost for? "); yearnb = scan.nextint(); costincrement = (y-x)/ 9.0; totalcost = x + costincrement*(yearnb-1); System.out.println("The cost for year 5 is " + fmt.format(totalcost));

11 4. Write a program called RandomWordGenerator that creates a String object S consisting of the characters abercd in this order. Your program should then create and print out a new String S1 consisting of exactly 4 unique characters that are randomly selected from S. Note that S1 is not allowed to have duplicate characters and should be composed of distinct characters only. For instance, the value bbae is not a valid value for S1 since it contains two b characters. On the other hand, baer is a valid value for S1 as no two characters are identical in that value. Sample run 1: Output string S1: bear Sample run 2: Output string S1: care import java.util.random; public class RndWordGenerator { public static void main(string[] args) { String str = "abercd"; Random rnd = new Random(); String str1 = ""; int rndindex; rndindex = rnd.nextint(str.length()); str1 = str1 + str.charat(rndindex); str = str.substring(0, rndindex) + str.substring(rndindex+1); rndindex = rnd.nextint(str.length()); str1 = str1+str.charat(rndindex); str = str.substring(0, rndindex) + str.substring(rndindex+1); rndindex = rnd.nextint(str.length()); str1 = str1 + str.charat(rndindex); str = str.substring(0, rndindex) + str.substring(rndindex+1); rndindex = rnd.nextint(str.length()); str1 = str1 + str.charat(rndindex); str = str.substring(0, rndindex) + str.substring(rndindex+1); System.out.println("Output string S1: " + str1);