CS170 (005): Introduction to Computer Science Exam 2

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

Question: Total Points: Score:

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

Instructor: Yu Wang 11/16/2012

Question: Total Points: Score:

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

Question: Total Points: Score:

Question: Total Points: Score:

Place your name tag here

Question: Total Points: Score:

Question: Total Points: Score:

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

Question: Total Points: Score:

Question: Total Points: Score:

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Question: Total Points: Score:

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

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

Question: Total Points: Score:

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Question: Total Points: Score:

Question: Total Points: Score:

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

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

Question: Total Points: Score:

CS 1301 Exam 1 Fall 2010

CS 190 Exam 2 Fall 2014

CS 1301 Exam 1 Fall 2010

CS 101 Spring 2007 Midterm 2 Name: ID:

Arrays. Eng. Mohammed Abdualal

CS 1301 Exam 3 Spring 2014

Exam Percentage: / 55 = %

CS 155 Exam 2 Spring 2015

CS170 Introduction to Computer Science Midterm 2

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

CS 1301 Exam 1 Spring 2011

Question: Total Points: Score:

CS 110 Exam 2 Fall 2011

CS 110 Exam 2 Spring 2011

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

CS 1301 Exam 1 Fall 2011

CS 1301 Exam 2 Fall 2014

CS 2316 Exam 4 Fall 2012

CS 1301 Exam 1 Fall 2014

CS 190 Exam 1 Fall 2010

CS 101 Exam 2 Spring Id Name

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

CS 1301 Exam 1 Fall 2014

COS 126 General Computer Science Spring Written Exam 1

CS 2316 Exam 4 Fall 2011

CS Exam 2 - Spring 2010

CS 1301 Exam 1 Spring 2014

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

CS 1301 Exam 1 Fall 2013

CS 101 Fall 2006 Midterm 1 Name: ID:

Pace University. Fundamental Concepts of CS121 1

King Saud University College of Computer and Information Sciences Computer Science Department

CS 1301 Exam 3 Fall 2014

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

CS 113 PRACTICE FINAL

Practice Midterm 1. Problem Points Score TOTAL 50

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 1301 Exam 1 Fall 2013

Midterm Exam 2 CS 455, Spring 2013

CS 1301 Exam 2 A Fall 2015

CS 1301 Exam 1 Spring 2014

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

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

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

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

Key Java Simple Data Types

Your (printed!) Name: CS 1803 Exam 3. Grading TA / Section: Monday, Nov. 22th, 2010

CS 2316 Exam 1 Spring 2014

CS 1316 Exam 1 Summer 2009

Java Simple Data Types

CS 1301 Exam 3 Spring 2012

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CS Exam 3 - Spring 2010

Introduction to Computer Science Unit 2. Notes

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

CS 1803 Fall 2010 Exam 1

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

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

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

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

CS Exam 3 - Spring 2010

Problem Score Max Score 1 Syntax directed translation & type

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CS 1301 Exam 2 Fall 2013

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

Java Reference Card. 1. Classes. 2. Methods. 3. Conditionals. 4. Operators

Give one example where you might wish to use a three dimensional array

C Sc 227 Project 1: Three Main Methods

H212 Introduction to Software Systems Honors

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.

CIS 110 Introduction to Computer Programming Summer 2014 Final. Name:

Transcription:

CS70 (005): Introduction to Computer Science Exam Name (print): Instructions Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work Do NOT communicate with anyone other than the professor/proctor for ANY reason in ANY language in ANY manner. This exam is close notes, close books, and no calculator Turn off all mobile devices and put them away now. You cannot have them on your desk. Write neatly and clearly indicate your answers. What I cannot read, I will assume to be incorrect Remain in your seat until you are done. If you have a question raise your hand and wait until the instructor comes to you. Time: This exam has 6 questions on 3 pages, including this title. Please make sure all pages are included. You will have minutes to complete this exam; budget your time accordingly. Partial credit will be given, so if you are unable to complete a question at least give an attempt. Honor Code: I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Emory community. I have also read and understand the requirements and policies outlined above. Signature: Q Q Q3 Q4 Q5 Q6

) (0 pts) For Question, consider the code below from the file Examq.java Examq.java ===================================== import java.util.scanner; public class Examq { public static int numinputs; public static double exp(double base, int pow) { double result =.0; int i=0; while (i < pow) { result = result*base; i++; return result; public static double getbase() { System.out.print("Base? "); Scanner in = new Scanner(System.in); double b = in.nextdouble(); numinputs++; return b; public static int getpow() { System.out.print("Pow? "); Scanner in = new Scanner(System.in); int p = in.nextint(); numinputs++; return p; public static void main(string [] args) { double base = getbase(); int pow = getpow(); double result = exp(base,pow); System.out.println("Result: " + result); System.out.println("numInputs: " + numinputs);

a) List the names of all local variables in the program above (If there are multiple local variables with the same name, you can list them just once) result, i, in, b, p, base, pow b) List the names of all parameters variables in the program above (If there are multiple parameter variables with the same name, you can list them just once) args, base, pow c) List the names of the methods in the program above exp, getbase, getpow, main d) What would the output to the console be for the program if the user inputs 0 when prompted for the base and when prompted for the pow? Base? 0 Pow? Result: 00 numinputs:

) (4 pts) Searching and Sorting Consider the following list for part a. 8 4 30 8 65 0 30 5 a) Write out the values in the list that will be inspected during a sequential (linear) search for the value 65. If a number is accessed more than once, list it more than once.,8,4,30,,8,65 Consider the following list for part b and c 5 8 8 30 30 4 65 0 b) Write out the values in the list that will be inspected during a binary search for the value 7 30,,,5

c) The 3 sets of list below show the sequence of a list of numbers when it is sorted using one of the sorting algorithms we studied. For each set of lists label it with the algorithm that was used to sort the initial list. The first list in each sequence is the initial array i. List Sequence Sorting Algorithm 8 8 8 Selection Sort ii. 8 8 8 8 8 Bubble Sort iii. 8 8 8 Insertion sort

3) ( pts) For this question fill in the class named Examq3 and put the methods from part a and b in this class a) Fill in the method reverse below. The method should create a new array with the elements in reverse order. The array should not modify the array referenced by the parameter value. The method returns the reversed array as its return value. For this question you may NOT use the methods in the java.util.arrays class. b) Fill in the main method for the class. The main method should extract the command line arguments into an array. Using the method you wrote in part (a) print this array in reversed order. For this question you may NOT use the methods in the java.util.arrays class. public class Examq3 { public static double [] reverse(double [] list) { // TODO: Write a method that reverse the order of the // list array and returns the reversed array // NOTE: The values in the parameter list must remain // unchanged! double [] rev = new double[list.length]; for (int i=0; i < list.length; i++) { rev[list.length--i] = list[i]; return rev;

public static void main(string [] args) { double [] argslist; // TODO: Set the values in argslist // the command line arguments argslist = new double[args.length]; for (int i=0; i < args.length; i++) { argslist[i] = Double.parseDouble(args[i]); double [] reversed = reverse(argslist); // TODO: print out the elements in the reversed // array. Separate each element by either a space // or a new line for (int i=0; i < reversed.length; i++) { System.out.println(reversed[i] + " ");

4) (8 pts) Write Java statements for each of the directions below. a) Use a single statement to declare an array of double values named array and initialize it to hold 5 values: 6., 7.3, 5.4,.,.0 double [] array = {6., 7.3, 5.4,.,.0; b) Write a method named nexttolast that takes an array of doubles as a parameter. The method should set the next to last value of the array to 3.4. If the array only has one element, then the method should set the last value of the array to 3.4. public static void nexttolast (double [] x) { if (x.length == ) { x[0] = 3.4; else { x[x.length - ] = 3.4;

c) Write a method named evenaverage that takes an array of integers as a parameter. The method should calculate the average of all of the elements in the array with an even index. This calculation should be returned by the method. public static double evenaverage (int [] x) { double sum = 0.0; int nelems = 0; for (int i=0; i < x.length; i+= ) { sum += x[i]; nelems++; if (nelems > 0) { return sum / nelems; else { return 0;

5) (6 pts) Write the output that the following code will generate to the console. Assume the code compiles as written. public class Examq5 { public static int var = 0; public static boolean var = false; public static int method(int var) { if (var == false) { var -=5; else { var += 5; return var; System.out.println("var in method = " + var); return var; public static void method() { var = ; var = Examq5.var - 3; System.out.println("var in method = " + var); public static boolean method3(int var, boolean b) { var = Examq5.var - ; System.out.println("var in method3 = " + var); if (b == true && var > 5) { System.out.println("Good"); return true; else { System.out.println("Bad"); return false; public static void main(string [] args) { int a = method(var); int var = 7; System.out.println("a = " + a); System.out.println("var = " + var); boolean b = method3(var, true); System.out.println("b = " + b); System.out.println("var = " + var); method();

Console Output ================= var in method = 5 a = 5 var = 7 var in method3 = 9 Good b = true var = 7 var in method = -

6) Two dimensional arrays (0 pts) Background: The array diagonal refers to elements along a line extending from the top- left corner of the array to the bottom- right corner. For example: If you have a 3x3 D array defined as: int [][] a = { {,, 3, {4, 5, 6, {7, 8, 9 ; The diagonal will be the values, 5, 9 For this question you may assume that the D array parameter is "square." This means that the width is the same as the length of the D array. a) Write a method named sumdiagonal that takes a D array of integers as a parameter. The method should return the sum of all elements on the diagonal of the D array. public static int sumdiagonal ( int [][] x) { int sum = 0; for (int i=0; i < x.length; i++) { sum += x[i][i]; return sum;

b) Write a method named sumwithoutdiagonal that takes a D array of integers as a parameter. The method should return the sum of all elements in the array excluding the elements on the diagonal. You may use the sumdiagonal method from part (a) to help if needed. (Assume the methods are class methods within the same class) public static int sumwithoutdiagonal (int [][] x) { int sum = 0; for (int i=0; i < x.length; i++) { for (int j=0; j < x[i].length; j++) { sum += x[i][j]; sum -= sumdiagonal(x); return sum;