Peer Instruction 6 Java Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Kernighan/Ritchie: Kelley/Pohl:
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
©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.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
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.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Computer Science 210 Computer Organization Arrays.
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.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Java Programming Language Lecture27- An Introduction.
ARRAYS.
Functions + Overloading + Scope
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 7: User-Defined Functions II
Computer Science 210 Computer Organization
Foundations of Programming: Arrays
New Structure Recall “average.cpp” program
CNG 140 C Programming (Lecture set 10)
Engineering Innovation Center
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Methods and Parameters
Object-Oriented Programming (OOP) Lecture No. 32
Computer Science 210 Computer Organization
CSC 253 Lecture 8.
Arrays, For loop While loop Do while loop
CSC 253 Lecture 8.
Can store many of the same kind of data together
An Introduction to Java – Part I, language basics
Week 9 – Lesson 1 Arrays – Character Strings
Lecture 18 Arrays and Pointer Arithmetic
Arrays in Java What, why and how Copyright Curt Hill.
160 Exam 2 Prep.
Can store many of the same kind of data together
Object Oriented Programming in java
Array and Method.
Arrays Syntax: type variableName[size];
Topics discussed in this section:
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Can store many of the same kind of data together
A Wide Array of Possibilities
Suggested self-checks: Section 7.11 #1-11
Java SE 7 One and Multi Dimensional Arrays Module 6
Arrays Arrays A few types Structures of related data items
Java Programming Language
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Arrays.
Peer Instruction 4 Control Loops.
Two-Dimensional Arrays
Multidimensional Arrays Section 6.4
Arrays 3/4 June 3, 2019 ICS102: The course.
Presentation transcript:

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[] = new short[4]; char cArray = {‘a’, ‘b’, ‘c’, ‘d’}; double dArray[] = new double {11.1, 22.2, 33.3}; String sArray[] = new String [] {"Java ", "Fortran", "C++"}; A) will not compile, cannot specify size B) is correct, initialization is implicit 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 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 are the correct values of iArray after running the code fragment below? // Code fragment int iArray[] = {2, 3, 4, 5, 6}; myMethod(iArray); // Method definition static void myMethod(int array[]) { for (int i=1; i<array.length-1; i++) array[i] *= 2; } 2, 3, 4, 5, 6 2, 6, 8, 10, 6 2, 6, 8, 10, 12 4, 6, 8, 10, 6 None of the above B) is correct, arrays passed by reference and can be changed NOTE: code above does not modify first and last element! Array Manipulation cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

cs163/164: Peer 6 - Java Arrays - Fall Semester 2016 Which of the following correctly declares a method with an array parameter? public void computeAverage0(int array[]) { … } public void computeAverage1(double []array) { … } public void computeAverage2(int array []) { … } public void computeAverage3(double[] array ) { … } All of the above E) is correct, doesn’t matter where the parentheses are Array Parameters cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

cs163/164: Peer 6 - Java Arrays - Fall Semester 2016 Which of the following correctly declares a method that returns an array? public double[] computeAverage() { … } public double[10] computeAverage() { … } public double computeAverage() { … } public double*10 computeAverage() { … } public double[][] computeAverage() { … } is correct, 1D array cannot specify size does not return an array, just a double is ridiculous, cannot combine operators and data types is correct, 2D array Returning Arrays cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

cs163/164: Peer 6 - Java Arrays - Fall Semester 2016 Which invocation correctly passes the character array cArray to the method? myMethod(cArray[]); myMethod(cArray); myMethod(cArray[8]); myMethod(char cArray[]); All of the above A) square brackets not allowed or required, compiler knows it’s an array B) is correct C) cannot specify size, works for any size by using .length D) method declaration specifies type not invocation E) only A) is correct, not all of the above Passing Arrays cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

cs163/164: Peer 6 - Java Arrays - Fall Semester 2016 Select the best description for the following code which ‘copies’ one array to another: // Code fragment char array0[] = {‘a’, ’b’, ’c’}; char array1[] = new char[3]; array1 = array0; Copies the contents of array0 to array1 Copies the reference to array0 to array1 Copies the name of array0 to array1 Does not even compile! would be nice, but does not work, either write loop or use System.arraycopy is correct, just makes another reference to the same array, not very useful is ridiculous is incorrect, no problem with the code shown NOTE: allocation of array1 is a waste of time! Array Copying cs163/164: Peer 6 - Java Arrays - Fall Semester 2016