Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS111 Introduction to Computer Science

Midterm 2

Fall 2023

Section 1: Weeks 1-4 (35 points)

1) (8 points) For each of the following scenarios, identify the best data type. Choose from any primitive data type, as well as String. Give a 1-line justification of why it is the best data type for the variable.

a) (2 points) Price of a product

Data Type:

Explanation:

b) (2 points) Rutgers NetID (the ID comprised of letters and numbers)

Data Type:

Explanation:

c) (2 points) Age

Data Type:

Explanation:

d) (2 points) Letter Grade

Data Type:

Explanation:

2) (15 points) Consider the following code snippet.

a) (10 points - 2 points each row) What are the values of m and n at the end of each iteration? Fill in the table below.

Iteration

m

n

0

0

54321

1

2

3

4

5

b) (5 points) What is the above code snippet doing in terms of n? Explain in 1-2 sentences.

3) (12 points - 3 points each) Consider the following code snippet. What is the output of this code snippet with the following inputs?

a)  x=0, y=0                    

b)  x=2, y=3                    

c)  x=3, y=2                    

d)  x=2, y=0                    

Section 2: Arrays (35 points)

1) (15 points) Write a snippet of code that creates an integer array of size 10 and populates each entry using the formula 2+3*index. For example, index 5 will contain the value 2 + 3 * 5=17. Print out each array entry on a new line. You DO NOT need to include the headers (public class/public static void main).

2) (10 points) Consider the following code snippet and line numbers.

a)   (2 points) What is line 1 doing?

b) (2 points) What does arr[r].length represent inline 5? What is the numerical value?

c) (2 points) What is happening inline 6?

d) (4 points) In 1-2 sentences, describe what the code snippet is printing? What is the actual output?

3) (10 points) Write a code snippet that finds and prints the minimum value in a 2-dimensional array of integers. Assume the array name is arr. You DO NOT need to include the headers (public class/public static void main).

Section 3: Input/Output

1. (5 points) Write a program that reads two inputs from the command line as integer values and displays the smallest of the two inputs. Assume the inputs are integers.

2) (10 points) What is the output of the program below when executed with the command java M2 < input.txt?

Output:

3) (5 points) Write the command to execute the program in question 2 and save the output in a file named output.txt.

Section 4: Functions (30 points)

1) (5 points) Consider the following function below.

a) (1 point) What is the name of the function?

b) (1 point) What are the inputs for this function?

c) (1 point) What is the return type?

d) (2 points) What would we write in the main method to call this function with the base as 2 and the exponent as 4?

2) (15 points) averageOf3

a) (10 points) Write a function averageOf3 that takes in 3 separate integers as parameters, and returns the average of the three numbers. Include the whole method signature

(modifiers, return type, method name, inputs) and the body of the method.

b) (5 points) Write 1-2 lines of code that will call your function with the inputs 2, 3, and 5, and print out the result.

3) (10 points) Write a function calculateWage that takes in a number of hours worked and an hourly rate as parameters. The function returns the total wage. For example, if the 5 hours are worked and the hourly rate is 13.50, the function will return 67.50.

Section 5: Recursion

1) (15 points) Consider the following recursive function foo() as well as the line numbers.

a) (2 points) Which line(s) handles the base case?

b) (3 points) What is the outcome if foo(0,0) is called?

c) (4 points) What are the values returned for foo(4,2) and foo(5,3) respectively?

d) (6 points) Briefly (1-2 lines) explain the purpose of the function foo().

2) (15 points) Answer the following questions given the recursive function mysteryFunc.

a) (5 points) What is printed when the following code snippet is executed?

b) (5 points) What is printed when the following code snippet is executed?

c) (5 points) Describe, in a sentence, what mysteryFunc computes.