Download presentation
Presentation is loading. Please wait.
1
Peer Instruction 4 Control Loops
2
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
What value is printed for the loop variable when the code shown below runs? int loop = 0; while (loop <= 11) { loop++; System.out.println(loop); } 0 to 9 0 to 10 1 to 10 1 to 11 None of the above Correct answer is D, loop executes with i = 0..10, but print is after increment so Bonus: What if the post increment is inside print! Answer: The code prints instead of While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
3
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
What value is printed for the loop variable when the code shown below runs? int loop = 0; while (loop <= 11) { loop++; System.out.println(loop); } 0 to 9 0 to 10 1 to 10 1 to 11 None of the above Correct answer is D, loop executes with i = 0..10, but print is after increment so Bonus: What if the post increment is inside print! Answer: The code prints instead of While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
4
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
What value is printed for the loop variable when the code shown below runs? int loop =0; while (0 <= loop <= 8) { loop++; } System.out.println(loop); 9 10 11 12 Will not compile! Correct answer is C, condition fails when i is 11, so loop body is not executed, but i is incremented. Bonus: How many times is condition evaluated? Answer: 11 times, 10 times true and 1 time false! While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
5
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
What value is printed for the loop variable when the code shown below runs? int loop =0; while (0 <= loop <= 8) { loop++; } System.out.println(loop); 9 10 11 12 Will not compile! Correct answer is C, condition fails when i is 11, so loop body is not executed, but i is incremented. Bonus: How many times is condition evaluated? Answer: 11 times, 10 times true and 1 time false! While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
6
How many times will the body of the ‘do while’ loop execute?
byte loop = -5; do { loop++; } while (loop > 0); 0 times 1 time 2 times 128 times Infinite loop! Correct answer is B, only one time, do while always executes at least once. Do While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
7
How many times will the body of the ‘do while’ loop execute?
byte loop = -5; do { loop++; } while (loop > 0); 0 times 1 time 2 times 128 times Infinite loop! Correct answer is B, only one time, do while always executes at least once. Do While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
8
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
What is printed below? for (int i = 0; i<10; ++i) { System.out.println(i); } 0 to 9 0 to 10 1 to 9 1 to 10 None of the above Correct answer is C, adds exclamation points one at a time until string length is 10. String Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
9
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
What is printed below? for (int i = 0; i<10; ++i) { System.out.println(i); } 0 to 9 0 to 10 1 to 9 1 to 10 None of the above Correct answer is C, adds exclamation points one at a time until string length is 10. String Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
10
How many lines does the nested loop shown below print?
for (int row = 0; row < 10; row += 2) { for (int col = 0; col < 50; col += 10) { System.out.println(row + "," + col) { } 20 times 25 times 36 times 500 times Different each time it runs! Nested Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
11
How many lines does the nested loop shown below print?
for (int row = 0; row < 10; row += 2) { for (int col = 0; col < 50; col += 10) { System.out.println(row + "," + col) { } 20 times 25 times 36 times 500 times Different each time it runs! Nested Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
12
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
Which of the following correctly prints the entire contents of the string? String s = "Programming is fun!"; for (int i=1; i< s.length(); i++) { System.out.print(s.charAt(i)); } for (int i=1; i<= s.length(); ++i) { System.out.print(s.charAt(i)); } for (int i=0; i<= s.length(); i++) { System.out.print(s.charAt(i)); } for (int i=0; i< s.length(); ++i) { System.out.print(s.charAt(i)); } None of the above Correct answer is D, must start at zero and end at length – 1. String Enumeration cs163/164: Peer 4 - Control Loops - Fall Semester 2016
13
cs163/164: Peer 4 - Control Loops - Fall Semester 2016
Which of the following correctly prints the entire contents of the string? String s = "Programming is fun!"; for (int i=1; i< s.length(); i++) { System.out.print(s.charAt(i)); } for (int i=1; i<= s.length(); ++i) { System.out.print(s.charAt(i)); } for (int i=0; i<= s.length(); i++) { System.out.print(s.charAt(i)); } for (int i=0; i< s.length(); ++i) { System.out.print(s.charAt(i)); } None of the above Correct answer is D, must start at zero and end at length – 1. String Enumeration cs163/164: Peer 4 - Control Loops - Fall Semester 2016
14
What is the last line printed by the ‘for’ loop shown below?
for (String s = "Hello"; s.length() <= 10; s += "!") { System.out.println(s); } Hello Hello! Hello!!!!! !!!!! None of the above Correct answer is C, adds exclamation points one at a time until string length is 10. String Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
15
What is the last line printed by the ‘for’ loop shown below?
for (String s = "Hello"; s.length() <= 10; s += "!") { System.out.println(s); } Hello Hello! Hello!!!!! !!!!! None of the above Correct answer is C, adds exclamation points one at a time until string length is 10. String Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016
16
Methods and Parameters
Peer Instruction 5 Methods and Parameters
17
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Which statement is a valid invocation of a method with and int and float parameter? myMethod(int i = 12, float f = 2.3); myMethod((int) 12, (float) 2.3f); myMethod(int i, float f); myMethod(int, float); myMethod(12.0, 2.3f); cannot specify data type, cannot have initializer is correct, but type casts are redundant cannot specify data type, compiler already knows is ridiculous, not actual parameters, just data types first parameter cannot be double, second is okay NOTE: actual parameters are those used to invoke method, also called arguments Actual Paramaters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
18
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Which statement is a valid invocation of a method with and int and float parameter? myMethod(int i = 12, float f = 2.3); myMethod((int) 12, (float) 2.3f); myMethod(int i, float f); myMethod(int, float); myMethod(12.0, 2.3f); cannot specify data type, cannot have initializer is correct, but type casts are redundant cannot specify data type, compiler already knows is ridiculous, not actual parameters, just data types first parameter cannot be double, second is okay NOTE: actual parameters are those used to invoke method, also called arguments Actual Paramaters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
19
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Which line of code uses the integer return value from the method correctly? System.out.println(calculateInteger()); int myInteger = calculateInteger(); double myDouble = (5.0 * calculateInteger()) / ; double myDouble = Math.max(calculateInteger(), 1234); All of the above Answer is E), anywhere a literal or variable of type int can be used you can have an int return value. Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
20
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Which line of code uses the integer return value from the method correctly? System.out.println(calculateInteger()); int myInteger = calculateInteger(); double myDouble = (5.0 * calculateInteger()) / ; double myDouble = Math.max(calculateInteger(), 1234); All of the above Answer is E), anywhere a literal or variable of type int can be used you can have an int return value. Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
21
What are the limitations of single return value from a method?
A single primitive (byte, int, float, double, char, boolean, ...) A single primitive or an array of primitives A single class, (String, Scanner, ...) A single class or an array of classes All of the above Answer is E), thus it’s not much of a limitation! Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
22
What are the limitations of single return value from a method?
A single primitive (byte, int, float, double, char, boolean, ...) A single primitive or an array of primitives A single class, (String, Scanner, ...) A single class or an array of classes All of the above Answer is E), thus it’s not much of a limitation! Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
23
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Given the code below, what is output by the two print statements, in order of execution? // Code fragment int value = 6; printSquare(value); System.out.println(value); public static void printSquare(int value) { value = value * value; } 6, 6 36, 6 36, 36 6, 36 None of the above Answer is B), value variables are completely difference memory locations, parameter is squared but not returned NOTE: Naming the actual and formal parameters the same is misleading! Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
24
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Given the code below, what is output by the two print statements, in order of execution? // Code fragment int value = 6; printSquare(value); System.out.println(value); public static void printSquare(int value) { value = value * value; } 6, 6 36, 6 36, 36 6, 36 None of the above Answer is B), value variables are completely difference memory locations, parameter is squared but not returned NOTE: Naming the actual and formal parameters the same is misleading! Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
25
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
How many activation records are on the stack when executing code in Math.sin? // code fragment in main foo(1.0); public static void foo(double d) { d += bar(d * d) ; } public static void bar(double d) { d *= Math.sin(Math.PI); 1 2 3 4 E) is correct: main, myMethod0, myMethod1, Math.sin What if bar called foo? What of bar called bar? Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
26
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
How many activation records are on the stack when executing code in Math.sin? // code fragment in main foo(1.0); public static void foo(double d) { d += bar(d * d) ; } public static void bar(double d) { d *= Math.sin(Math.PI); 1 2 3 4 E) is correct: main, myMethod0, myMethod1, Math.sin What if bar called foo? What of bar called bar? Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
27
Peer Instruction 6 Java Arrays
28
cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly declares, allocates, and initializes an array? int iArray[5] = {1, 2, 3, 4, 5}; short sArray[4] = new short[4]; char cArray = {‘a’, ‘b’, ‘c’, ‘d’}; double dArray[] = new double {11.1, 22.2, 33.3}; String sArray[] = {"Java ", "Fortran", "C++"}; A) will not compile, cannot specify size B) will not compile, cannot specify size C) will not compile, variable is not an array D) will not compile, add square brackets or remove ‘new double’ E) is correct, long form of initialization, new String[] can be omitted Array Declaration cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
29
cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly declares, allocates, and initializes an array? int iArray[5] = {1, 2, 3, 4, 5}; short sArray[4] = new short[4]; char cArray = {‘a’, ‘b’, ‘c’, ‘d’}; double dArray[] = new double {11.1, 22.2, 33.3}; String sArray[] = {"Java ", "Fortran", "C++"}; A) will not compile, cannot specify size B) will not compile, cannot specify size C) will not compile, variable is not an array D) will not compile, add square brackets or remove ‘new double’ E) is correct, long form of initialization, new String[] can be omitted Array Declaration cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
30
cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly prints out the fourth element of iArray? System.out.println(iArray + 4); System.out.println(iArray[4]); System.out.println([4]iArray); System.out.println(iArray(4)); None of the above is ridiculous, cannot add array and integer accesses the fifth element, otherwise okay actually works in ‘C’ language, not java, wrong index again cannot use parentheses instead of brackets, wrong index again E) is correct Array Access cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
31
cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly prints out the fourth element of iArray? System.out.println(iArray + 4); System.out.println(iArray[4]); System.out.println([4]iArray); System.out.println(iArray(4)); None of the above is ridiculous, cannot add array and integer accesses the fifth element, otherwise okay actually works in ‘C’ language, not java, wrong index again cannot use parentheses instead of brackets, wrong index again E) is correct Array Access cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
32
cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly increments all the elements of iArray? for (int i=0; i < iArray.length(); i++) iArray[i]++; for (int i=1; i <= iArray.length; i++) iArray[i]++; for (int i=0; i < iArray.length;) iArray[i++]++; iArray[0..iArray.length]++; iArray++; has extra parentheses loop index starts at second element is correct would be nice, but Java doesn’t have .. range operator is ridiculous Array Loops cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
33
cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly increments all the elements of iArray? for (int i=0; i < iArray.length(); i++) iArray[i]++; for (int i=1; i <= iArray.length; i++) iArray[i]++; for (int i=0; i < iArray.length;) iArray[i++]++; iArray[0..iArray.length]++; iArray++; has extra parentheses loop index starts at second element is correct would be nice, but Java doesn’t have .. range operator is ridiculous Array Loops cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
34
Two-Dimensional Arrays
Peer Instruction 7 Two-Dimensional Arrays
35
Declaration and allocation of two-dimensional (2D) arrays.
Which statements correctly declare and allocate a 2D integer array with 2 rows and 4 columns? int iArray[2][4]; int iArray = new int[2][4]; int iArray[][] = new int[4][2]; int iArray[][] = new int[2][4]; int iArray[][] = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 } }; 2) and 4) 3) and 5) 1) and 5) 4) and 5) 2) and 5) 4) and 5) are correct, 1) does not allocate, 2) is not an array, 3) is backwards, so the answer is E) 2D Array Declaration cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016
36
Declaration and allocation of two-dimensional (2D) arrays.
Which statements correctly declare and allocate a 2D integer array with 2 rows and 4 columns? int iArray[2][4]; int iArray = new int[2][4]; int iArray[][] = new int[4][2]; int iArray[][] = new int[2][4]; int iArray[][] = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 } }; 2) and 4) 3) and 5) 1) and 5) 4) and 5) 2) and 5) 4) and 5) are correct, 1) does not allocate, 2) is not an array, 3) is backwards, so the answer is E) 2D Array Declaration cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016
37
Accessing elements in a 2D array (Part 1)
What is the value of cArray[2][1] after the following code has executed? ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ char cArray[][] = new char[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cArray[i][j] = (char) ('a' + j + i); The answer is cArray[2][1] = (char) (‘a’ ) = ‘d’, so the answer is D), show the whole array on the board. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016
38
Accessing elements in a 2D array (Part 1)
What is the value of cArray[2][1] after the following code has executed? ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ char cArray[][] = new char[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cArray[i][j] = (char) ('a' + j + i); The answer is cArray[2][1] = (char) (‘a’ ) = ‘d’, so the answer is D), show the whole array on the board. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016
39
Accessing elements in a 2D array (Part 2)
Which array element correctly accesses the highlighted value in the array below? 123 234 345 456 567 678 789 890 111 222 333 444 555 666 777 987 876 765 654 543 432 321 210 iArray[4][3] iArray[3][4] iArray[3][2] iArray[2][3] iArray[3][3] The answer is C), since row and column are zero-based and Java is row-major. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016
40
Accessing elements in a 2D array (Part 2)
Which array element correctly accesses the highlighted value in the array below? 123 234 345 456 567 678 789 890 111 222 333 444 555 666 777 987 876 765 654 543 432 321 210 iArray[4][3] iArray[3][4] iArray[3][2] iArray[2][3] iArray[3][3] The answer is C), since row and column are zero-based and Java is row-major. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.