Peer Instruction 4 Control Loops.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
The switch Statement, DecimalFormat, and Introduction to Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java Unit 9: Arrays Declaring and Processing Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
August 6, 2009 Data Types, Variables, and Arrays.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Methods.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Java Programming Language Lecture27- An Introduction.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.
Information and Computer Sciences University of Hawaii, Manoa
Functions + Overloading + Scope
Test 2 Review Outline.
Arrays Chapter 7.
Hassan Khosravi / Geoffrey Tien
The switch Statement, and Introduction to Looping
Lecture 5: Some more Java!
Chapter 6 More Conditionals and Loops
Primitive Data, Variables, Loops (Maybe)
Programmazione I a.a. 2017/2018.
Engineering Innovation Center
Methods and Parameters
Advanced Programming Behnam Hatami Fall 2017.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Arrays, For loop While loop Do while loop
Peer Instruction 6 Java Arrays.
An Introduction to Java – Part I, language basics
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays in Java What, why and how Copyright Curt Hill.
160 Exam 2 Prep.
IFS410 Advanced Analysis and Design
Can store many of the same kind of data together
Arrays Syntax: type variableName[size];
Arrays Chapter 7.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Chapter 6 Methods.
CISC124 Labs start this week in JEFF 155. Fall 2018
CMPE212 – Reminders The other four assignments are now posted.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Comparing Python and Java
Two-Dimensional Arrays
C++ Array 1.
Review for Midterm 3.
Corresponds with Chapter 5
Presentation transcript:

Peer Instruction 4 Control Loops

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 1..11. Bonus: What if the post increment is inside print! Answer: The code prints 0..10 instead of 1..11. While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016

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 1..11. Bonus: What if the post increment is inside print! Answer: The code prints 0..10 instead of 1..11. While Loops cs163/164: Peer 4 - Control Loops - Fall Semester 2016

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

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

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

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

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

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

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

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

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

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

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

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

Methods and Parameters Peer Instruction 5 Methods and Parameters

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

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

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()) / 0.12345; 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

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()) / 0.12345; 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

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

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

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

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

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

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

Peer Instruction 6 Java Arrays

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

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

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

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

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

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

Two-Dimensional Arrays Peer Instruction 7 Two-Dimensional Arrays

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

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

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’ + 1 + 2) = ‘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

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’ + 1 + 2) = ‘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

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

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