Sunteți pe pagina 1din 11

CSE 110 Midterm Exam 2 Practice A KEY

Fall 2013

NAME (please print in LEGIBLE block letters!):


Your University ID Number:
The name of the student to your LEFT:
The name of the student to your RIGHT:

Please use a pen for all answers.


No books, notes, or electronic devices may be used during the exam.
The last page of the exam contains a summary of helpful information, like Pep/8 opcodes.
Any CHEATING will result in an F as well as being written-up on academic dishonesty. Dont
place anything in a location where it might indicate that you are copying.
The score for a perfect exam (without extra credit) is 100 points.
NO-BS Bonus: If you do not know the answer to a problem and you leave it COMPLETELY
BLANK, you will receive 1 point for that problem (or part of a problem). If you write anything
in that space and it is wrong, you will receive a 0.

QUESTION VALUE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

10
10
10
6
6
6
6
6
15
5
5
6
4
10
6

TOTAL

111
1

SCORE

1. (10 points)
Draw the binary search tree whose elements have been inserted into an initially empty tree in the
following order:
50, 72, 96, 107, 26, 12, 11, 9, 2, 25

2. (10 points)
The following code fragment compiles and runs without error. What output does it produce?
2

int num = 1;
int max = 20;
while (num < max)
{
if (num % 2 == 0)
System.out.println(num);
num = num + 1;
}

Output: 2, 4, 6, 8, 10, 12, 14, 16, 18

3. (10 points)
Assume that you have a computer system that has 3500K available for user processes. This memory is initially empty, and is assigned to programs in contiguous blocks using first-fit allocation.
Consider the following sequence of events:

1. Process 1 arrives and requests 1000K of memory


2. Process 2 arrives and requests 250K of memory
3. Process 1 terminates
4. Process 3 arrives and requests 300K of memory
5. Process 4 arrives and requests 1500K of memory

After this sequence of events, how much free memory is available for use? Can the computer
accommodate another process that requires 1000K of memory? Why or why not?
Total memory free: 1450K. However, this free memory is broken into two blocks (700K and 750K),
so the new process will NOT be able to run.

4. (6 points)
Which of the following hexadecimal sequences correctly represents the following Pep/8 instruction?
Load the value 31 into Register 0
(a) E10011
(b) C1001F
(c) E1001F
(d) C0001F

ANSWER:

5. (6 points)
Consider the following list of processes, with their total CPU times:

Process
------P1
P2
P3
P4
P5

CPU Time
-------3
8
4
5
2

Assume that the processes have arrived in the order shown above, but none of them has started to
execute yet. Draw a diagram to show how these processes will be executed under a shortest-job-first
(SJF) strategy and under a round-robin (RR) strategy with a time slice of 2.

SJF: P5, P1, P3, P4, P2

Round-Robin: P1, P2, P3, P4, P5, P1, P2, P3, P4, P2, P4, P2

6. (6 points)
Briefly describe the difference between the SCAN and LOOK disk scheduling algorithms.
SCAN examines every cylinder up to the ends of the disk (the very first and very last cylinder)
before changing direction. LOOK changes direction after it has processed the final request in its
current direction.

7. (6 points)
Briefly explain how stacks and queues work.
Stacks are last-in, first-out (LIFO), while queues are first-in, first-out (FIFO).

8. (6 points)
List one advantage and one disadvantage of virtual memory.

Advantage: cheaper than real RAM, allows you to run more programs at once

Disadvantage: slower than real RAM, more complex

9. (15 points)
Write some Java code to do the following:

1. Read in two words representing passwords and store them in the String variables below.
2. Print out the number of characters in the shorter of the two words.

For example, if the user enters open and sesame, your code should print out 4 (the number of
characters in open).

Scanner input = new Scanner(System.in);


String first;
String second;
System.out.print("Enter a word: ");
first = input.nextLine();
System.out.print("Enter a second word: ");
second = input.nextLine();
if (first.length() < second.length())
System.out.println(first.length());
else
System.out.println(second.length());

10. (5 points)
What is the result of performing a 2-bit right circular shift on 10011001?
(a) 11001100
(b) 11100110
(c) 00100110
(d) 01100110

10011001 becomes

ANSWER:

100110 becomes 01100110

11. (5 points)
Which of the following is NOT a part of the memory hierarchy?
(a) RAM
(b) The hard disk
(c) The control unit
(d) None of the above

ANSWER:

The memory hierarchy consists (in order from fastest to slowest) of registers, cache, RAM (main
memory), and secondary storage (hard disks, CD/DVD-ROMs, etc.). The control unit is part of
the CPU, but it determines which instruction will be loaded next; it has nothing to do with actually
storing data.

12. (6 points)
The SBU 2010 CPU contains a pipeline that uses six stages: Fetch, Decode, Load Arguments,
Execute, Writeback, and Update. This pipeline processes instructions at a rate of one stage per
clock cycle. Assume that you have 10 instructions to process, and that the pipeline is empty to
begin with. Calculate the number of clock cycles that you will save by using pipelining
instead of using a non-pipelined approach. You must show your calculations (including any
formulas you use) in order to receive any credit.
Without pipelining: instructions * stages = 10 * 6 = 60 cycles
With pipelining: instructions + stages - 1 = 10 + 6 - 1 = 15 cycles
45 cycles are saved by using pipelining.

13. (4 points)
Consider the following Java String:
String s = "abcdefghijklmno"
Write a Java expression that will return the substring "fghijk" from this list.
s.substring(5, 11)

10

14. (10 points)


Consider the following list of values:
3, 46, 75, 66, 48, 58, 95, 70, 20, 8
We will use selection sort to rearrange the values in this data set. Show what the data set looks
like after each round of the algorithm.
3, 46, 75, 66, 48, 58, 95, 70, 20, 8
3, 46, 75, 66, 48, 58, 8, 70, 20, 95
3, 46, 20, 66, 48, 58, 8, 70, 75, 95
3, 46, 20, 66, 48, 58, 8, 70, 75, 95
3, 46, 20, 8, 48, 58, 66, 70, 75, 95
3, 46, 20, 8, 48, 58, 66, 70, 75, 95
3, 46, 20, 8, 48, 58, 66, 70, 75, 95
3, 8, 20, 46, 48, 58, 66, 70, 75, 95
3, 8, 20, 46, 48, 58, 66, 70, 75, 95
3, 8, 20, 46, 48, 58, 66, 70, 75, 95
3, 8, 20, 46, 48, 58, 66, 70, 75, 95

15. (6 points)
Given an array of integers named x and an integer variable named total that has already been
declared and initialized to 0, write some code that places the sum of all the elements of the array
x into total. Declare any additional variables that you need.

for (int i = 0; i < x.length; i = i + 1) // 4 points


{
total = total + x[i]; // 2 points
}

11

S-ar putea să vă placă și