Practice Problems Sol

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CSc

 2010  Principles  of  Computer  Science,  Fall  2013  


Practice  Problems  for  Midterm  
 
1. For each Java expression in the left hand column, indicate its value in the right
hand column. Be sure to show a constant of the appropriate type. For example, 7.0
rather than 7 for a double, Strings in quotes, etc.

3* 2 + 4 * 3 ________18__________

17 % 9 - 20 % (26 / 7) ________6___________

2 + 5 + "2" + 2 + 5 _________”7225”_____

2.5 + 5 / 2 + (3 / 0.5) ________10.5________

3 * (2 + 4) * 2 / 10 ________3___________

2. Multiple Choice (Answer in bold font)


(a) An example of an output device that interfaces between computers and humans is

a) a keyboard.
b) a mouse.
c) a speaker.
d) a microphone.

(b) Which one of the following typically provides data persistence without electricity?

I. The CPU’s memory


II. The hard disk
III. Secondary storage

a) I, II only
b) I, III only
c) II, III only
d) I, II, III

(c) High-level programming languages

a) Are made up primarily of ones and zeros


b) Are independent of the underlying hardware
c) Are not standardized
d) Use syntax that is close to the underlying hardware’s instruction set
(d) What is one of the benefits of using a high-level programming language like Java?

a) Its syntax is very similar to the hardware instruction set


b) No tools other than a text editor are required for programming
c) Statements in the high-level language are just like English
d) Problems solved in a high-level language are independent of the underlying
computer hardware

(e) What is the difference between an editor and a compiler?

a) An editor converts program files into an executable program; a compiler allows program
files to be written and stored
b) An editor allows program files to be written and stored; a compiler produces an organized
list of files
c) An editor allows program files to be written and stored; a compiler produces an indexed
database of terms and keywords
d) An editor allows program files to be written and stored; a compiler converts
program files into an executable program

(f) Every statement in Java must be terminated with

a) the semi-colon character ;.


b) a carriage return.
c) System.out.println().
d) an escape sequence.

(g) These two lines of code do not produce the same output. Why?

System.out.println(7 + 3);
System.out.println("7 + 3");

a) Arithmetic calculations cannot take place within the println method call.
b) In fact, the two statements do produce the same output.
c) The quotes cause the second expression to be treated as a string.
d) Because there are no escape characters.

(h) Which statement starts the declaration of a class in Java?

a) public static void main(String[] args)


b) public class Classname
c) System.out.println("Hello, World!");
d) Java class
(i) Which Java statement does not contain an error?

a) System.out.print(;
b) System.out.print()
c) System.out.printl();
d) System.out.println();

(j) What kind of error is created by the following code snippet?

System.outt.println("Hello");

a) No error: the code is correct


b) Logic error: the program will run until it comes to this statement
c) Syntax error: the program will not compile
d) Exception: the statement will generate an exception

(k) Which one of the following code snippets compiles without errors and displays the output
“Hello Good Day!” on the screen?

a) System.out.print("Hello ");
System.out.println("Good Day!");

b) System.out.print("Hello );
System.out.println("Good Day!");

c) System.out.print("Hello ");
System.out.println("Good Day!")

d) System.out.print("Hello ");
System.out.println(Good Day!");

(l) The source code for a Java program is stored in a file

a) that ends with a .class suffix


b) that can have any valid file name
c) that ends with a .java suffix
d) that has no suffix

(m) A Java "class" file

a) contains Java source code


b) contains instructions to the Java virtual machine
c) is an internal file created by the Integrated Development Environment (IDE)
d) is the translation of the Java source code into C++
3. Written Solution (Output shown in bold font)
 
What  is  the  output  of  running  the  following  Java  programs  (Be  precise  in  
showing  the  output  including  spaces  and  new  lines).  
 
public class P1 {
public static void main(String args[]) {
int num=9465,d1,d2,d3,d4;

d4 = (num / 1000) % 10;


d3 = (num / 100) % 10;
d2 = (num / 10) % 10;
d1 = (num / 1) % 10;

System.out.print("d4 = ");
System.out.println(d4);
System.out.print("d3 = ");
System.out.println(d3);
System.out.print("d2 = ");
System.out.println(d2);
System.out.print("d1 = ");
System.out.println(d1);
}
}  
   
 
d4  =  9  
d3  =  4  
d2  =  6  
d1  =  5  
 
public class P2 {
public static void main(String args[]) {
double p=8.0, q=6.0, r=12.0, num1, num2;

num1 = p + q / r;
num2 = p * q + p * r % 2;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
}
}

num1 = 8.5
num2 = 48.0
4. Write  a  Java  program  that  computes  the  maximum  and  average  of  3  numbers  
that  are  read  from  keyboard  input.  A  sample  run  is  shown  below:  

Welcome to Maximum Calculator

Enter first number (integer): 80


Enter second number (integer): 85
Enter third number (integer): 88

Maximum Score is: 88


Average Score is: 84.33

import java.util.Scanner;
import java.lang.Math;

public class Max {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println(“Welcome to Maximum Calculator“);

System.out.print(“Enter first number (integer): “);


int num1 = sc.nextInt();
System.out.print(“Enter second number (integer): “);
int num2 = sc.nextInt();
System.out.print(“Enter third number (integer): “);
int num3 = sc.nextInt();

int max = Math.max(Math.max(num1,num2),num3);


double avg = (num1+num2+num3)/3.0;

System.out.println(“\nMaximum Score is: “+max);


System.out.println(“Average Score is: “+avg);

sc.close();

}
5. You have been asked to develop an algorithm to calculate the total cost of a purchase
order that contains several T shirts. The cost of each T shirt and the tax rate is known.
The standard shipping charge for the entire order is $5.75, and the special delivery charge
is $23.65. In addition, there is no tax on the shipping cost. Which of the following is the
correct pseudocode for the required algorithm?

a) For each T shirt on the purchase order:


Order cost = order cost + T shirt cost
Total purchase order cost = order cost + tax rate + 5.75

b) For each T shirt on the purchase order:


Order cost = order cost + T shirt cost
If standard shipping
Shipping cost = 5.75
Else
Shipping cost = 23.65
Total purchase order cost = order cost * tax rate + shipping cost

c) If standard shipping
Shipping cost = 5.75
Else
Shipping cost = 23.65
For each T shirt on the purchase order:
Order cost = order cost + T shirt cost + shipping cost
Total purchase order cost = order cost * tax rate

d) If special delivery
Shipping cost = 5.75
Else
Shipping cost = 23.65
For each T shirt on the purchase order:
Order cost = order cost + T shirt cost
Total purchase order cost = order cost * tax rate + shipping cost
6. Imagine you are planning to purchase a new cable TV dish. You are considering two cable TV
dishes that have different purchase prices. Each channel service provider charges a different rate
for each month that the cable TV dish is used. To determine which cable TV dish is the better
buy, you need to develop an algorithm to calculate the total cost of purchasing and using each
cable TV dish. What are all of the inputs that you need for this algorithm?

a) The cost of each cable TV dish and the rate for each month for using each cable TV dish
b) The cost of each cable TV dish and the number of months provided with each cable TV dish
c) The cost of each cable TV dish, the rate per month for using each cable TV dish, and the
number of months provided with each cable TV dish
d) The cost of each cable TV dish, the rate per month for using each cable TV dish,
and the number of months you would use the cable TV dish.

7. What is the purpose of the following algorithm?

someNum = 0
Repeat the following steps 50 times
Input variable1
if variable1 > someNum
someNum = variable1
Print someNum

a) To find the largest of 50 numbers


b) To print out the 50 numbers
c) To find the smallest of 50 numbers
d) To search for a particular number among 50 numbers

8. Write an algorithm to find minimum and maximum of n numbers.

Input:
Get a value for n, the size of the list
Get values for A1, A2, …, An
Method:
Set largestSoFar to A1
Set smallestSoFar to A1
Set i to 2
While (i <= n) do
If (Ai > largestSoFar) Then
Set largestFoFar to Ai
If (Ai < smallestSoFar) Then
Set smallestSoFar to Ai
Add 1 to i
End of loop
Output:
Print largestSoFar and smallestSoFar

You might also like