Download presentation
Presentation is loading. Please wait.
1
CS Week 6 Jim Williams, PhD
2
This Week Exam 1 - Thursday Team Lab: Branches & Maze Lecture: Loops
3
Midterm Exam 1 What is the location of your exam? 3650 Humanities
6210 Social Science 105 Psychology Other (due to conflicts) notified via What is the location of your exam?
4
Midterm Exam - Thursday
Bring ID and #2 pencil Exam seating directly in front/behind, 1 empty seat to each side Multiple choice - focus on tracing/explaining Java Review Questions posted on Piazza
5
Number Systems Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 0, 1
6
Decimal = 302
7
Binary = 14
8
Binary 8 4 2 1 = 5
9
Convert from Binary to Decimal
40 41 42 other 101010
10
Convert from Decimal to Binary
11001 11101 10011 other 29
11
Names for Numbers of Bits
bit nibble (4 bits) byte (8 bits) 2 bytes (16)
12
Hexadecimal (group bits by nibble)
0000 = 0 0001 = 1 0010 = 2 0011 = 3 0100 = 4 0101 = 5 0110 = 6 0111 = 7 1000 = 8 1001 = 9 1010 = A 1011 = B 1100 = C 1101 = D 1110 = E 1111 = F
13
What character is this? 0000 0000 0100 0001 A B Unicode:
@ Unicode: 0x003E 62 > 0x003F 63 ? 0x @ 0x A 0x B 0x C
14
Color 183, 1, 1 B70101 Red, Green, Blue (RGB)
183, 1, 1 B70101 Red, Green, Blue (RGB)
15
Incrementing m int m = 2; m = m + 1; //add one to m store in m
m += 1; //compound operator m++; //increment operator (post) ++m; //increment operator (pre)
16
Value of i after this executes?
int i = 5; i = i++; 5 6 other
17
Value of m after this executes?
int i = 5; int k = 6; int m = i k; 11 12 other
18
Difference between if and while
boolean done = true; if ( done) { System.out.println( done); } while ( done) {
19
Which loop is the better choice?
int a = 1; while ( a < 5) { System.out.println("hello"); a++; } while for other for ( int a = 1; a < 5; a++) { System.out.println("hell o"); }
20
3 loops - Different Purposes
do { //indefinite - body executes at least once } while ( !finished); while ( !finished) { //indefinite - body may never execute } for ( int i=1; i < 5; i++) { //definite
21
Race Track
22
How many times will this execute?
1 until num > 0 until num <= 0 Scanner input = new Scanner( System.in); int num; int sum = 0; do { num = input.nextInt(); sum += num; } while ( num > 0);
23
Body of While will execute...
boolean finished = false; while ( !finished) { String line = input.nextLine(); if ( line.equals("done")) { finished = true; } else { System.out.println("echo:" + line); } until "done" is input never forever other?
24
How many "hello"? for ( int a = 0; a <= 5; ++a) {
System.out.print("hello "); } 4 5 6 other?
25
What is the value of i after this?
int i = 4; int j = 3; while ( i <= 15) { i = 2 * -i + j; System.out.println( "i:" + i + " j:" + j); } 5 13 -23 49
26
How many "bye"? for ( int i = 1; i <= 5; i++) {
for ( int j = 2; j < 6; j++) { System.out.print("bye "); } 5 9 20 other?
27
What does this print? for ( int i = 1; i <= 5; i++) {
1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 4,1 4,2 4,3 5,1 5,2 5,3 1,1 2,1 3,1 1,2 2,2 3,2 1,3 2,3 3,3 1,4 2,4 3,4 1,5 2,5 3,5 for ( int i = 1; i <= 5; i++) { for ( int j = 1; j <= 3; j++) { System.out.print(i+","+j+" "); } System.out.println();
28
What is the value of count?
int count = 0; for ( int i = 1; i < 4; i++) { for ( int j = 6; j > 3; j--) { count += 1; } System.out.println("count=" + count); 3 9 12 other
29
What is the value of sum? 3 9 12 other int sum= 0;
boolean done = false; for ( int i = 1; i < 4 && !done; i++) { for ( int j = 6; j > 3; j--) { if ( j > 4) continue; sum += 1; } if ( i == 3) break; System.out.println("sum=" + sum); 3 9 12 other
30
What does this do? for ( char ch = 'A'; ch < 'Z'; ch += 3) {
System.out.print( ch); }
31
What do a and b possibly have in common?
public static void main(String []args) { mA(); mB(); } static void mA() { int a = 10; static void mB() { int b; System.out.println( b); may be the exact same memory location looking at b will reveal a (unintentially) nothing Error A is the best answer. This example is typically discussed with a memory diagram intended to illustrate how local variables are allocated on the stack. Local variables are a part of the stack frame that are allocated when a method is called and discarded when the method ends. Since mA() has 1 int variable allocated (pushed on stack) and then discarded (popped off stack) then mB() may reuse that same memory area. However, this depends on how the Java virtual machine implements stack frames. Thinking through this possibility is intended to help develop a deep understanding of local variables.
32
Is result true or false? true Integer m = 5; Integer n = 5; false
boolean result = m == n; true false error A is correct. The fine print at the provided link says for Integer instances based on int literals between and 127 will have the same reference for the same int literal value. The intention of this example is to emphasize experimenting, discovering short illustrative examples and an introduction to the Java Language Specification. Part of relevant details: If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b. Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. Java Language Specification
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.