CMSC 131 (Summer) Week 04 Study Questions Flashcards

1
Q

Go back to the loop questions from the previous week and implement them all using for-loops instead of while loops!

A

Go back to the loop questions from the previous week and implement them all using for-loops instead of while loops!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What will the output be:

A

Output: x = 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

int x = 7;
int y = x++ + 3;
int z = 7 * –x + 4;
System.out.println(“x = “ + x + “, y = “ + y + “, z = “ + z);

A

Output: x = 7, y = 10, z = 53

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
int x = 5;
x += 7;
x -= 2;
x /= 2;
x *= 8;
x %= 11;
System.out.println(x);
A

Output: 7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a chart showing the precedence of all of the following operators: = , < , ==, &&, ||, !=, ++, + (addition), +=, –, *

A
\++, --
*
\+
<  
==, !=
&amp;&amp;
||
=, +=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If two operators occur in the same expression, and they are on the same level in the precedence chart (it is a “tie”), how do you decide which operator gets evaluated first?

A

Go from left to right (unless they are assignment operators, in which case go from right to left.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Using parentheses indicate the order in which each of the following expressions will be evaluated or state that the expression represents an invalid expression. You may assume that all variables are of type int.
    x / y * z % w
    x ++ + y ++
    x + y + z != w % p * 2
    x < y || z > m &amp;&amp; y <= 4
A

((x / y) * z) % w
(x++) + (y++)
((x + y) + z) != ((w % p) * 2)
(x < y) || ((z > m) && (y <= 4))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

[We skipped this one in Fall 2013.] List as many reasons as you can for why it is so important to invest lots of time in the design of a large-scale software project before the project is coded. (5 reasons were listed in class.)

A
Here are some of the things a good design can improve:
Efficiency (speed)
Efficiency (memory)
Ease of coding
Ease of debugging
Ease of expansion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write psuedocode for the following program. The program will read in strings that represent names of students. It will keep prompting the user for more and more names until the user enters “STOP”. After that, the program will display the number of names entered, the name that is first alphabetically, and the name that is last alphabetically.

A

Let count = 0.
Do
Prompt “Enter a name: “
Input name from user
If count is still 0,
Let first = name
Let last = name
Otherwise if name != “STOP”
If name precedes first alphabetically
Let first = name
If last precedes name alphabetically
Let last = name
Increment count
While name is not “STOP”, keep looping
Output “Number of names entered = “ count
Output “First name alphabetically is” first
Output “Last name alphabetically is” last

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write psuedocode for a program that computes the number of digits in an integer. For example, if the user enters 1792, the output will be “4”.

A
/* There is an easier way with logarithms */
Prompt "enter an integer"
Input number from user.
Let compareValue = 10
Let counter = 1
While number >= compareValue
    Increment counter
    Let compareValue = compareValue * 10
Output: "Number of digits is" counter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write psuedocode for a program that reads a sequence of integer values and decides whether or not it is a decreasing sequence. The program will first read in the number of values to process, followed by the values themselves. The output will be “Yes” if the sequence is decreasing, and “No” otherwise.

A
Prompt "How many values"
Input numValues from user
Let decreasingSequenceFlag =  true
Prompt "Enter a value: "
Input recentValue
Let counter = 1
While counter <= numValues
    Prompt "Enter a value: "
    Input newValue
    if  newValue >= recentValue
        decreasingSequenceFlag = false
    recentValue = newValue
    increment counter
if decreasingSequenceFlag = true
     output "YES"
otherwise
     output "NO"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the following code fragments are OK, and which will cause problems?

int x = 52;
double y = x;

double x = 14;
int y = x;

int x = 7;
long y = x;

long x = 17L;
short y = x;

A

OK

NOT OK

OK

NOT OK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Show how to use “explicit casting” to force the troublesome examples in the previous question to work.

A

For part b:
int y = (int) x;

For part d:
short y = (short)x;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly