CMSC 131 (Summer) Week 04 Study Questions Flashcards
Go back to the loop questions from the previous week and implement them all using for-loops instead of while loops!
Go back to the loop questions from the previous week and implement them all using for-loops instead of while loops!
What will the output be:
Output: x = 2
int x = 7;
int y = x++ + 3;
int z = 7 * –x + 4;
System.out.println(“x = “ + x + “, y = “ + y + “, z = “ + z);
Output: x = 7, y = 10, z = 53
int x = 5; x += 7; x -= 2; x /= 2; x *= 8; x %= 11; System.out.println(x);
Output: 7
Write a chart showing the precedence of all of the following operators: = , < , ==, &&, ||, !=, ++, + (addition), +=, –, *
\++, -- * \+ < ==, != && || =, +=
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?
Go from left to right (unless they are assignment operators, in which case go from right to left.)
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 && y <= 4
((x / y) * z) % w
(x++) + (y++)
((x + y) + z) != ((w % p) * 2)
(x < y) || ((z > m) && (y <= 4))
[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.)
Here are some of the things a good design can improve: Efficiency (speed) Efficiency (memory) Ease of coding Ease of debugging Ease of expansion
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.
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
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”.
/* 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
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.
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"
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;
OK
NOT OK
OK
NOT OK
Show how to use “explicit casting” to force the troublesome examples in the previous question to work.
For part b:
int y = (int) x;
For part d:
short y = (short)x;