CSC 1051 Arrays - Review questions

Similar documents
Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays in Java Multi-dimensional Arrays

Arrays, Part 3: Multidimensional arrays

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Arrays in Java Using Arrays

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

ESC101 : Fundamental of Computing

CS 200 Arrays, Loops and Methods Jim Williams, PhD

Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Happy Cinco de Mayo!!!!

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Computer Science is...

This page intentionally left blank

Tutorial 06. Conditional statement: if then, if else, switch

Java Arrays. What could go wrong here? Motivation. Array Definition 9/28/18. Mohammad Ghafari

CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name:

MACS 261J Final Exam. Question: Total Points: Score:

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

CIS November 27, 2018

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Array. Array Declaration:

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Final Exam. CSC 121 Spring 2016 TTH. Lecturer: Howard Rosenthal. May 17, 2016

Happy Cinco de Mayo!!!!

AP CS Unit 7: Arrays Exercises

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Chapter 2 Exercise Solutions

array Indexed same type

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Place your name tag here

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

H212 Introduction to Software Systems Honors

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

CS 113 MIDTERM EXAM 2 SPRING 2013

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Administrative Stuff CIS Last Time. Midterm 2 today in-lab this week dups. Assignment 10. November 27, 2018

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

Final Exam. CSC 121 Fall 2015 MW. Lecturer: Howard Rosenthal. Dec. 16, 2015

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

1. What does the following code fragment write to the monitor?

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

CIS November 14, 2017

Advanced Computer Programming

Chapter 6 Single-dimensional Arrays

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CS61B Lecture #6: Arrays

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Lab Assignment Three

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

COSC 1010 Introduction to Computer Programming

Final Exam. COMP Summer I June 26, points

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

CSC 1051 Algorithms and Data Structures I. Final Examination December 20, Name: KEY. Question Value Score

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Laboratorio di Algoritmi e Strutture Dati

Data Structures CSCI C343, Fall 2016 [B]

CS 112 Introduction to Programming

CS 112 Introduction to Programming

THE JAVA FOR STATEMENT

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

AP Computer Science A Unit 7. Notes on Arrays

2 marks. class q1c{ class point{ int p,q; point(int p, int q){ this.p=p; this.q=q; } void printpoint(){ System.out.println(this.p+" "+this.

Unit 10: Sorting/Searching/Recursion

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Admin. CS 112 Introduction to Programming. Recap: Arrays. Arrays class. Array Return (call) Array Return (declare) q Exam 1 Max: 50 Avg/median: 32

This midterm has about 4 questions more than the actual midterm of 200 pts will have

Introduction to Arrays

University of Palestine. Mid Exam Total Grade: 100

CS 307 Midterm 2 Spring 2008

CS 132 Compiler Construction, Fall 2012 Instructor: Jens Palsberg Multiple Choice Exam, Nov 6, 2012

Principles of Programming. Chapter 6: Arrays

Final Exam. CSC 121 Fall 2015 TTH. Lecturer: Howard Rosenthal. Dec. 15, 2015

CS 132 Midterm Exam Spring 2004

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

AP COMPUTER SCIENCE A DIAGNOSTIC EXAM. Multiple Choice Section Time - 1 hour and 15 minutes Number of questions - 40 Percent of total grade - 50

Solutions to Section #5

INHERITANCE AND TYPE HIERARCHIES. Prof. Chris Jermaine

Transcription:

CSC 5 Arrays - Review questions ) Given the following declarations: int a = 2; int b = 3; int c = 5; double x = 2.5; double y =.; double z = 4.32; double[] list = 2., 3.8,.4}; Show what value is assigned by each of the following assignments, or state that there is an error, clearly indicating through the use of a decimal point whether values are of type int or double. In the case of the list array, show its contents and circle the element modified by the assignment. a) z = a / b; b) z = (double) a / b; c) c = (int) x * 5; d) c = x + 2; e) c = a % b; f) z = list[]; g) z = list; h) list = x; i) list[] = a; j) list[] = x; k) list[a] = x; l) list[y] = ; 2) Draw a diagram showing the array contents after execution of the following code fragment: int[] list = new int[4]; for (int i=; i < list.length; i++) list[i] = i * ; 3) Draw a diagram showing the array contents after execution of the following code fragment: int[] list = new int[3]; for (int i=; i < list.length; i++) list[i] = list.length i; 4) Assume the list array has been declared and initialized as in the previous question. Write a code fragment that uses a loop to add 2 to each array element. 5) Assume the list array has been declared and initialized as in the previous question. Write a code fragment that creates another array called listtoo of the same size and copies all the elements from list to listtoo BACKWARDS: a) Without using a loop b) Using a loop

6) Assume an array list has been declared and initialized with some values. Write a code fragment that creates another array called biglist of twice the size and then copies each element from list to biglist twice, as follows: a) Copying the entire list first forward and then backwards. Example: list biglist 5 7 6 6 7 2 8 8 9 3 b) Copying each element twice in consecutive locations. Example: biglist list 3 2 8 3 8 4 2 5 2 6 6 7 6 8 7 9 7 7) Draw a diagram showing the array contents after execution of the following code fragment: int[] a = 2, 5, 3}; int[] b = new int[8]; for (int i=; i < a.length; i++) b[i] = a[i]; for (int i=a.length; i < b.length; i++) b[i] = 5;

8) Draw a diagram showing the array contents after execution of the following code fragment: int[]a = 4,2,7,9}; int[]b = new int[]; for (int i=; i < a.length; i++) b[i] = (a[i] * ) + i; for (int i=a.length; i < b.length; i++) b[i] = a[a.length-] * ; Exercises on 2D Arrays. Draw a picture showing the array contents after execution of the following code fragments. int count = ; int[][] table = new int[2][3]; for (int i=; i < 2; i++) for (int j=; j < 3; j++) table[i][j] = count; count++; } ANSWER: table 2 2 3 4 5 6 Example int[][] tablea = new int[3][3];

for (int i=; i < 3; i++) tablea[i][i] = i; char[][] tableb = new char[4][4]; String sample = OPEN THIS FIRST ; for (int i=; i < 4; i++) for (int j=; j < 4; j++) tableb[i][j] = sample.charat(i+j); int[][] tablec = new int[3][4]; for (int i=; i < tablec.length; i++) for (int j=; j < tablec[i].length; j++) tablec[i][j] = i * 2 + j; 2. Create a test program to test each of the above code fragments and to show the array contents after execution. Use these programs to verify your answers to the previous question. Note that you will need to write some extra code to display the array contents (see, for example the boldfaced code below). EXAMPLE: int count = ; int[][] table = new int[2][3]; for (int i=; i < 2; i++) for (int j=; j < 3; j++) table[i][j] = count; count++;

} //print out the array contents for (int i=; i < table.length; i++) /// processing rows for (int j=; j < table[].length; j++) //processing columns System.out.print( table[i][j] + " "); System.out.println(); // done with row, go to new line } 3. Write a method fill2d() that creates and returns a 2D array of dimensions n x m, where n, m are int parameters, and the array is filled with consecutive numbers starting at. For example, if invoked as fill2d(2,3) it should return the array: 2 2 3 4 5 6 4. Write a method with one int parameter n that creates and returns a two dimensional array of size n x n that consists of values of type double that are all set to zero, except the ones along the diagonal, which are all set to. So, for example, if the parameter is 3, the method should return an array with the following contents: 2 2 5. Download, compile, and run SodaSurvey.java (Chapter 8). Modify the code to add static method computeavg() that computes and returns the average of all the elements in a 2D array; the method should accept a single parameter, a two dimensional array of int, and it should return the average as a double. Use computeavg() in the main() method of SodaSurvey.java to compute and print the average of all the soda scores (this will be the overall average, not by soda or by person, as computed in the program). http://www.csc.villanova.edu/~map/5/chap8/sodasurvey.java