0% found this document useful (0 votes)
3 views6 pages

PracticeMidterm1 Soln

This document is a practice midterm exam for COSC 111: Computer Programming I, designed to help students prepare for their upcoming exam. It includes multiple choice, short essay, analysis, and coding questions, along with instructions on exam conduct and preparation. The exam covers various programming concepts and requires students to demonstrate their understanding of Java programming through practical coding tasks.

Uploaded by

Naman Pahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

PracticeMidterm1 Soln

This document is a practice midterm exam for COSC 111: Computer Programming I, designed to help students prepare for their upcoming exam. It includes multiple choice, short essay, analysis, and coding questions, along with instructions on exam conduct and preparation. The exam covers various programming concepts and requires students to demonstrate their understanding of Java programming through practical coding tasks.

Uploaded by

Naman Pahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COSC 111: Computer Programming I

PRACTICE MIDTERM EXAM 1

About this practice exam:

- The aim of this practice exam is to help you prepare for Midterm Exam 1.

- Several questions in this practice exam were taken straight from the lecture notes and
lab questions. However, this does not mean the actual exam will also include questions
copied from the lecture notes/lab. This also does not mean the topics/questions in this
practice exam are more important than other in the course.

- The actual exam questions will be at the same level of difficulty of the questions you
have already practiced on (classroom exercises, lecture notes, and lab work).

- The length of the actual exam will be determined based on the allowed exam time.

- To practice for the exam, do the following:


o Solve all exercises found in the course (classroom examples, lecture notes, and
lab exercises/assignments) up to the cut-off for the exam.
o If you still need more practice, consider the questions at the end of each chapter
in the course textbook.
o The exam will be on paper, so you need to practice writing code on paper.

Instructions
1. This exam has X pages. Ensure that you have a complete exam (check page numbers) and that
the exam is printed properly.
2. ALL questions are required except the bonus which is optional.
3. To help you keep track of time, the expected time for each part is provided at the beginning the
part.
4. No aids are permitted (e.g. laptops, tablets, programmable calculators, cell phones, course notes,
textbooks, etc.).
5. You are allowed one cheat-sheet (handwritten on one side of an A4 paper ). Your name must be
written on the cheat sheet. You cannot exchange cheat sheets with others during the exam .
6. Place your name, university number, and lab section on at least the first sheet of the exam paper.
7. Answer all questions on the exam paper only and hand in each sheet of the completed exam.
8. There is extra space at the end of the exam that you can use for solution or for scrap paper.
Part 1. Multiple Choice Questions (Circle correct answer) (6 marks – 9 minutes)
Circle ONE correct answer then COPY your choice (i.e. one of a, b, c, d, …, h) to the table below
Question # 1 2 3 4 5 6
Your Choice

I have used below 6 clicker questions we had in the course for the sake of demonstration.

1. What is the output? (note that Math class is imported by default)


double x = floor(2.3) + ceil (1.01);
[Link](x);

A. 3
B. 3.0
C. 4
D. 4.0
E. Error

2. Which of the following statements generates a random integer from 0 to 25 inclusive?


A. int x = [Link]() * 26;
B. int x = (int) [Link]() * 26;
C. int x = (int)( [Link]() * 26 );
D. int x = (int)( [Link]() * 25 );

3. What is the output?


char ch = '5';
int x = ch - '0';
[Link](x + 2);
A. 52
B. 502
C. 7
D. Error

4. What is the output?


String s1, s2;
Scanner in = new Scanner([Link]);

s1 = [Link](); // User enters: abc


s2 = [Link](); // User enters: abc

[Link](s1 == s2)
A. true
B. false
5. What is the output?
int x = 10;
if (x <= 10)
[Link]("A");
else {
[Link]("B");
[Link]("C");
}
A. A
B. B
C. ABC
D. AB
E. AC

6. What is the displayed on the screen?


int x = 5, y = 2;
x > y? "larger": "smaller";
A. larger
B. smaller
C. larger smaller
D. Error

Part2. Short Essay Questions: (6 marks – 12 minutes)

7. What is the difference between a variable and a literal? Given an example.

A variable in java is a location in the computer’s memory that is used to store data in a program.
Variables must be declared with a name and type in Java.

A literal is a constant value that appears directly in the program.

Example: int x = 4; //variable is x, literal is 4

8. What is the difference between integer division and normal (real) division in Java. Give an
example for each.

Normal division: is the regular mathematical division. In java, at least one of the two operands must
be a floating point number (double or float). Example: x = 3.1 / 2; //x is 1.55

Integer division: the same as normal division, but we throw away the remainder (or fraction). In java,
both operands must be integers. Example: y = 3/2; //y = 1
Part3. Analysis Questions: (6 marks – 14 minutes)

9. This code has one logic error and one syntax error. Circle and explain them, and suggest a way to fix
them.
Scanner in = new Scanner([Link]); //assume Scanner is imported.
int radius = [Link](); //user can input any integer
if(radius>0);
[Link]("Circumference: ", 2 * [Link] * radius);

Logic error: semi colon at the end if the if() condition may cause the resulting circumference to be
negative. The extra semicolon is considered as an empty statement following the if statement. Since we
are not using braces {}, only the empty statements will represent the body of the if statement, and the
following print statement will be executed regardless of the condition.
Fix: remove ; at end if the if() line

Syntax error: comma in the last statement. The println() method expects a single value or an expression
(i.e. multiple values combined using operators).
Fix: replace the comma “,” with a plus “+” (another fix is to use printf("Circumference: %f”,
2*[Link]*radius)

10. What is the output? Explain your answer (you could trace the code to explain how it works).
float f = 12.52F; //1
int i = (int)f; //2
[Link]("f is %.1f \n", f); //3
[Link]("i/5 is " + i/5); //4
Output:
f is 12.5
i/5 is 2
Explanation:
Statement 1 assigns a 12.52 to f.
Statement 2 casts the current value of f to an int, so i is assigned 12
Statement 3 prints f formatted to one decimal only (because of the use of %.1f placeholder)
Statement 4 uses integer division to compute i/5 which results in 2.
Part4. Short Coding Questions: (6 marks – 15 minutes)

In this part, you do not need to write complete programs; just provide the required code fragments. You
also do not need to declare or initialize any variable.

11. Write the following two expressions in Java and assign their values to any two variables.

double x = 4 / (3*(r+34)) - 9 * (a+b*c) + (3+d*(2+a)) / (a+b*d)

double y = 5.5 * [Link](r+2.5, 2.5+t)

12. Write code that prompts the user to enter a number and then checks whether that number is
divisible by 2 and 3, and whether a number is divisible by 2 or 3
Scanner input = new Scanner([Link]);
[Link]("Enter an integer: ");
int number = [Link]();
if (number % 2 == 0 && number % 3 == 0)
[Link](number + " is divisible by 2 and 3.");
if (number % 2 == 0 || number % 3 == 0)
[Link](number + " is divisible by 2 or 3.");
}
}
Part 5. Long coding Qu
estion: (6 marks – 25 minutes)

13. Write a program that reads the user’s salary (double) and marital status (single character: S for single
or M for married) then displays the taxes according to the rules in the table below. For example, a
married person whose salary is $40,000 should pay $12,000 in taxes (i.e. 30% of $40,000)

Salary Larger than $ 50,000 Between $35000 and $50000 Less than $ 35,000
Marital status Married Single Married Single Married Single
Taxes 40% of salary 50% of salary 30% of salary 40% of salary 20% of salary 30% of salary

Display an error message if the user enters an input that doesn’t start with M or S, i.e. when reading
the marital status, extract the first character from the user’s input. For example,
o All these inputs count as married: "M", "m", "married", "Married",
" M_any_characters "
o All these inputs count as single: "S", "s", "single", "Single",
" S_any_characters ".

Sample runs:

Enter your salary: 40000


Enter M=married, S=single: M
Tax: $12000.0
Enter your salary: 70000
Enter M=married, S=single: single and
happy
Tax: $35000.0
Enter your salary: 20000
Enter M=married, S=single: S
Tax: $6000.0
Enter your salary: 60000
Enter M=married, S=single: engaged
Invalid marital status!

Note: since this is taken from assignment A4 which is due after the midterm, I cannot provide a
sample solution here.

You might also like