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

Similar documents
CIS 1068 Program Design and Abstraction Spring2015 Midterm Exam 1. Name SOLUTION

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

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

1 Short Answer (10 Points Each)

AP COMPUTER SCIENCE A

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSC141, Computer Science I, Instructor: Dr. Zhen Jiang, Test 2

Repetition, Looping. While Loop

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

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

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

CSC240 Static method, array, and loop.

Operators Questions

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Question: Total Points: Score:

Place your name tag here

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Selenium Class 9 - Java Operators

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

H212 Introduction to Software Systems Honors

JAVA OPERATORS GENERAL

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

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

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Prof. Navrati Saxena TA: Rochak Sachan

1 Short Answer (5 Points Each)

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

Arithmetic Compound Assignment Operators

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

1. An operation in which an overall value is computed incrementally, often using a loop.

Java Programming Language. 0 A history

++x vs. x++ We will use these notations very often.

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Midterm Examination (MTA)

Question: Total Points: Score:

Loops. CSE 114, Computer Science 1 Stony Brook University

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Controls Structure for Repetition

CS 101 Spring 2007 Midterm 2 Name: ID:

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Top-Down Program Development

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

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Term 1 Unit 1 Week 1 Worksheet: Output Solution

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

1 Short Answer (2 Points Each)

CSE 142 Sample Midterm Exam #3

University of Palestine. Mid Exam Total Grade: 100

CS 101 Exam 2 Spring Id Name

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

Some Practice Midterm Problems

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

Arrays. Eng. Mohammed Abdualal

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

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

Chapter 3. Selections

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

Intro to Programming in Java Practice Midterm

Practice with variables and types

Flow of Control: Loops. Chapter 4

CSE 142 Sample Midterm Exam #3

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

Over and Over Again GEEN163

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

CIS October 19, 2017

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Fundamentals of Programming Data Types & Methods

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

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Ch. 6. User-Defined Methods

Practice Midterm 1. Problem Points Score TOTAL 50

CONDITIONAL EXECUTION

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

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CPSC 219 Extra review and solutions

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

Software Practice 1 Basic Grammar

University of Massachusetts Amherst, Electrical and Computer Engineering

CS180. Exam 1 Review

1 Short Answer (10 Points Each)

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

St. Edmund Preparatory High School Brooklyn, NY

STUDENT LESSON A12 Iterations

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Java Programming for Selenium

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Darrell Bethea May 25, 2011

Lara Technologies Special-Six Test

IEEE Floating-Point Representation 1

Question: Total Points: Score:

Final Examination Semester 2 / Year 2011

Transcription:

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1 Name SOLUTION Page Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Total 100 1 P age

1. Program Traces (41 points, 50 minutes) Answer the questions below about each snippet of code. For the sake of brevity, I have left off the lines "public class <ClassName> {" and "public static void main(string [] args) {" from most of these questions. Pretend they are there. a. (4 points) Number Calculation: what are the values of w, x, y, and z after these lines execute? int w; int x = 5, y = 8; int z = y % x; y += 1 + z; w x y z? 5 12 3 b. (3 points) Value of boolean variable and precedence order: what are the values of a, b, and c after these lines execute? boolean a; boolean b = 3 < 5 / 2 && true; boolean c = b (!b && 1 > 6 % 2); a c. (3 points) Type Compatibility: what are the values of a, b, and c after these lines execute? int a = 2 + 1 / 2; double b = 2.5 + 1 / 2; double c = 2.5 + 1.0 / 2;? a 2 b false true d. (1 point) String: what does this code display on the screen? c b c 2.5 3.0 int a = 25; boolean b = false; String s = ""; System.out.println(a + s + b); 25false e. (4 points) Array: what are the values of a, b, c, and d after these lines execute? int [ ] a; int [ ] b = null; int [ ] c = new int[4]; int [ ] d = {2, 3, 4; c[2] = 1; d[0] = d[1] d.length; a b c d? null 0 3 4 0 0 1 0 2 P age

f. (8 points) For each column, show what the code displays on the screen. Code Output: int x = 2; int y = 3; int z = 4; z=6 if(x + y > z) z = z + 2; System.out.println("z=" + z); int x = 2; int y = 3; boolean b = true; 2 if(b x > y) { System.out.println(x); else { System.out.println(b); int x = 1; int y = 2; boolean b = true; true boolean c = false; if(b && x >= y) { System.out.println(x); else if (c x < y) { System.out.println(b); else System.out.println(c); int [] x = {1, 3, 5; int [] y = {1, 2, 3, 4; if(x.length>y.length) { x[y.length-1] = y[y.length-1]; 10 else if (x[2] > y[2]) { y[3] = x[2]; else { y[1] = x[1] + x[2]; System.out.println(y[1]+y[2]+y[3]); booleanb = true; inti = 0; 1 while (b) { true b = i ++ <2; 2 System.out.println(i); true System.out.println(b); 3 false 3 P age

g. (18 points) What are the values of a, b, and c at the end of the code in each column? Code Value of a, b, and c int [] a = {3, -2, 4, 5; int b = 0, c = 0; for( b=0; b<a.length; b++) { a 10 5 3 2 c = c + a[b]; b 4 for( b=0; b<a.length; b++) { a[b] = c / (b+1); c 10 int [] a = {1, -2, 4, 3; int b = a[0]; int c = 0; for( c=0; c <a.length; c++) { a 1 1 4 4 if (a[c] > b) { b = a[c]; a[c] = b; b c 4 4 String a = "You-see-you- drive-you."; String u = "you"; String v = "they"; int b = a.indexof(u); String c = ""; a b -1 "." while(b>=0) { String word = a.substring(0,b); c = c + word + v; a = a.substring(b+u.length()); b = a.indexof(u); c = c + a; c "You-see-they-drive-they." 4 P age

2. Fill the blanks (10 points, 10 minutes) For the requirement specified in the below, complete the corresponding loop program so that the computer can print out the expected result. a. Write a program to print out the result of 1+2+3+4+ +99. int total = 0 ; inti=0; while (i<100){ total=total+i; i++; System.out.print(total); b. Write a program to print out the result of 1+2+4+8+ +4096. int total = 1 ; inti=1; while (i<3000){ i *= 2; total=total+ i ; System.out.print(total); c. Write a program that reads in an integer n from the keyboard, and displays whether n is a "perfect number" or not.a number is "perfect" if it is equal to the sum of all of its factors (not including itself as a factor, but including 1 as a factor). 6 is the first perfect number, because its factors are 1, 2, and 3, and 1+2+3 = 6. int n = keyboard.nextint(); int total = 1; for( int factor = 2 ; factor < n ; factor++) if (n%factor ==0) total += factor; System.out.println(n == total); 5 P age

3. Writing short programs (39 points, 40 minutes) For each problem below, write some Java code to solve it. You do not need to define a class or the main method, or import any classes. For this question, you should assume that all necessary classes (e.g., Scanner, Arrays) have been imported. Furthermore, assume that the following line precedes every question below, for you to use if you want: Scanner keyboard = new Scanner(System.in); hello hello hello a. (7 points) Write code that reads in a word W and a number N from the user (i.e., via keyboard). Your code should display W on N lines. For example, if the user enters "hello" for W and 3 for N, your code should display: String W = keyboard.nextline(); int N = keyboard.nextint(); for(int i = 0; i< N; i++) System.out.println(W); 6 P age

1x7=7 2x7=14 3x7=21 4x7=28 b. (7 points) Write code that reads in an integer N via the keyboard, and displays all the products of 7, from 1 x 7 up to N x 7. For instance, if the user enters 4, the program should display: int N = keyboard.nextint(); for( int i = 1; i<=n; i++) System.out.println(i+ x7= +i*7); 7 P age

c. (14 points) Write code that reads in a number C and a number R from the user (i.e., keyboard), and displays a figure with R rows and 2xC columns of "$" and characters. For instance, if the user enters a 5 for C and 4 for R, your program should display the following shape of "$"s: $$ $$ $$$ $$$ $$$$ $$$$ $$$$$$$$$$ int C = keyboard.nextint(); int R = keyboard.nextint(); for( int i = 0; i < R; i++){ int j; for(j = 0; j < C-(R-i-1); j++) System.out.print("$"); for(j = 0; j < 2*(R-i-1); j++) System.out.print(" "); for(j = 0; j < C-(R-i-1); j++) System.out.print("$"); System.out.println(); 8 P age

d. (11 points) Write code that reads in a number C and a number R from the user (i.e., keyboard), and displays a figure with R rows and C columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached. You may assume that both C and R are greater than 0. Here are some example calls to your method and their expected results: Input 3 6 5 3 4 1 Output 1, 4, 7, 10, 13, 16 2, 5, 8, 11, 14, 17 3, 6, 9, 12, 15, 18 1, 6, 11 2, 7, 12 3, 8, 13 4, 9, 14 5, 10, 15 1 2 3 4 1 3 1, 2, 3 for(int i = 1; i<= r; i++){ int num = i; for(int j = 1; j< c; j++){ system.out.print(num+, ); num+=r; system.out.println(num); 9 P age

4. Challenge: Writing a full Java program (10 points, 20 minutes) Write a complete Java program called CoPrime.java that reads in two integers and prints Co-Prime if the numbers are co-prime, and Not Co-Prime if they are not. Don t forget import statements, and declarations for the class and main method. Two integers are said to be co-prime if they share only one factor, the number 1. For instance, 8 and 9 are co-prime, because the factors for 8 are {1, 2, 4, 8, and the factors for 9 are {1, 3, 9. Only the number 1 is a factor for both 8 and 9. The numbers 6 and 9 are not co-prime, because 3 is a factor of both. Hint: Use the % operator to determine if one number is a factor of another. For instance, (x % y == 0) is true if y is a factor of x, and false otherwise. import java.util.scanner; public class MidtermC { public static void main(string args[]){ Scanner keyboard= new Scanner(System.in); int n1 = keyboard.nextint(); int n2 = keyboard.nextint(); int f; for(f =2; (n1%f!=0 n2%f!=0 ) && f<=((n1<n2)?n1:n2); f++); System.out.print(f>n1); 10 P age