Objective 7 - Java Static Two-Dimensional Arrays

Similar documents
AP Computer Science Java Mr. Clausen Program 9A, 9B

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Introduction to Java

Building Java Programs

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Basics of Java Programming Input and the Scanner class

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Building Java Programs

JAVA ARRAY EXAMPLE PDF

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

J a v a Quiz (Unit 3, Test 0 Practice)

(Eng. Hayam Reda Seireg) Sheet Java

AP Computer Science Static Methods, Strings, User Input

Object-Oriented Programming in Java

Sample CSE8A midterm Multiple Choice (circle one)

LAB4 Making Classes and Objects

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

Lecture 22: C Programming 4 Embedded Systems

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Using Files as Input/Output in Java 5.0 Applications

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Introduction to Object-Oriented Programming

Arrays in Java. Working with Arrays

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Section 6 Spring 2013

CS170 Lab 11 Abstract Data Types & Objects

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Introduction to Programming

Homework/Program #5 Solutions

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS114: Introduction to Java

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

In this Chapter you ll learn:

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Part I. Multiple Choice Questions (2 points each):

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

AP Computer Science Java Subset

Introduction to Java. CS 3: Computer Programming in Java

Arrays. Introduction. Chapter 7

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

How to Install Java onto your system

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Building Java Programs

Java CPD (I) Frans Coenen Department of Computer Science

Using Two-Dimensional Arrays

CMSC 202H. ArrayList, Multidimensional Arrays

An Incomplete C++ Primer. University of Wyoming MA 5310

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Java Classes. GEEN163 Introduction to Computer Programming

MR-(Mapreduce Programming Language)

Introduction to Data Structures

Java Crash Course Part I

Arrays in Java. data in bulk

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Explain the relationship between a class and an object. Which is general and which is specific?

Moving from CS 61A Scheme to CS 61B Java

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Building Java Programs

4.5 Symbol Table Applications

WRITING DATA TO A BINARY FILE

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Threads 1. When writing games you need to do more than one thing at once.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Data Structures using OOP C++ Lecture 1

Chapter 1 Java Program Design and Development

History OOP languages Year Language 1967 Simula Smalltalk

Inheritance, overloading and overriding

Manual For Using the NetBeans IDE

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Crash Course in Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Java Basics: Data Types, Variables, and Loops

Unit 6. Loop statements

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 2 Introduction to Java programming

Object-Oriented Programming in Java

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Example of a Java program

Java Programming Fundamentals

Java from a C perspective. Plan

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

B.Sc (Honours) - Software Development

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

sveltest: A testing language

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

public static void main(string[] args) { System.out.println("hello, world"); } }

Conditionals (with solutions)

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Transcription:

Ch 5 2D Arrays Multiple Choice Review Objective 7 - Java Static Two-Dimensional Arrays 33. Which of the following statements correctly declares a two-dimensional integer array? int Matrix[ ] = new int[5,4]; (B) int Matrix[ ]; Matrix = new int[5,4]; int Matrix[ ][ ] = new int[5][4]; (D) int Matrix[ ][ ]; Matrix = new int[5][4]; (E) Both choices C and D Exposure Java 2013, APCS Edition Chapter 10 Test Page 1 Updated: 05-16-13

34. What is the output of the program below? public class Java1034 public static void main(string args[ ]) int matrix[ ][ ]; matrix = new int[3][4]; for(int p = 0; p < 3; p++) for(int q = 0; q < 4; q++) System.out.print(matrix[p][q] + " "); System.out.println(); System.out.println(); 0 0 0 0 (B) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (D) Compile Error 35. Consider the mambo object declaration below. double mambo[ ][ ]; mambo = new double[4][5]; int r; // row index on mambo int c; // column index of mambo Which of the following statements stores the column length of mambo? mambo.length (B) mambo.rowlength mambo[r].length (D) mambo[c].length Exposure Java 2013, APCS Edition Chapter 10 Test Page 2 Updated: 05-16-13

36. What is the output of the program below? public class Java1036 public static void main(string args[ ]) int matrix[ ][ ]; matrix = new int[3][4]; for(int p = 0; p < 3; p++) for(int q = 0; q < 4; q++) System.out.print(matrix[q][p] + " "); System.out.println(); System.out.println(); 0 0 0 0 (B) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (D) Error message 37. Consider the mambo object declaration below. double mambo[ ][ ]; mambo = new double[4][5]; int r; // row index on mambo int c; // column index of mambo Which of the following statements stores the row length of mambo? mambo.length (B) mambo.rowlength mambo[r].length (D) mambo[c].length Exposure Java 2013, APCS Edition Chapter 10 Test Page 3 Updated: 05-16-13

Use the program below for questions 38-40. Each question will provide a different implementation of the createsquare method. The output shown is formatted for ease of reading. Technically, the columns will not line up so nicely. public class Java3840 public static void main (String args[]) int square[][] = new int[5][5]; createsquare(square); displaysquare(square); public static void createsquare(int[][] square) public static void displaysquare(int[][] square) for (int r = 0; r < 5; r++) for (int c = 0; c < 5; c++) System.out.print(square[r][c] + " "); System.out.println(); Exposure Java 2013, APCS Edition Chapter 10 Test Page 4 Updated: 05-16-13

38. What will be the output of program Java3840.java with the createsquare implementation below? public static void createsquare(int[ ][ ] square) int size = square.length; for (int r = 0; r < size; r++) int q = 1; for (int c = r; c < size; c++) square[r][c] = q; q++; 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 (B) 1 2 3 4 5 0 1 2 3 4 0 0 1 2 3 0 0 0 1 2 0 0 0 0 1 0 0 0 0 1 0 0 0 1 2 0 0 1 2 3 0 1 2 3 4 1 2 3 4 5 (D) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Exposure Java 2013, APCS Edition Chapter 10 Test Page 5 Updated: 05-16-13

39. What will be the output of program Java3840.java with the createsquare implementation below? public static void createsquare(int[ ][ ] square) int size = square.length; for (int r = 0; r < size; r++) int q = r; for (int c = 0; c < size; c++) square[r][c] = q + c; 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 (B) 1 2 3 4 5 0 1 2 3 4 0 0 1 2 3 0 0 0 1 2 0 0 0 0 1 0 0 0 0 1 0 0 0 1 2 0 0 1 2 3 0 1 2 3 4 1 2 3 4 5 (D) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Exposure Java 2013, APCS Edition Chapter 10 Test Page 6 Updated: 05-16-13

40. What will be the output of program Java3840.java with the createsquare implementation below? public static void createsquare(int[][] square) int size = square.length; int r = 0; int c = size / 2; square[r][c] = 1; for (int k = 2; k <= size*size; k++) if (k % size == 1) r++; else r--; c++; if (r < 0) r = size-1; if (c == size) c = 0; square[r][c] = k; 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 (B) 11 18 25 2 9 10 12 19 21 3 4 6 13 20 22 23 5 7 14 16 17 24 1 8 15 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 (D) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Exposure Java 2013, APCS Edition Chapter 10 Test Page 7 Updated: 05-16-13

Objective 8 - Storing Objects in a Static Array Use the class and program segment below for questions 41-42. class Student private String name; private int age; public Student(String n, int a) name = n; age = a; public void showdata() System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println(); Student students[] = new Student[numStudents]; for (int index = 0; index < numstudents; index++) System.out.print("Enter student's name ==> "); String name = stringinput.nextline(); System.out.print("Enter student's age ==> "); int age = intinput.nextint(); students[index] = new Student(name,age); 42. Which of the following describes the relationship of the objects? (B) (D) The program uses a Students array object of Student objects. The program uses a Student array object of Students objects. The program uses a Student object of Student array objects. The program uses a Students object of Students array objects. Exposure Java 2013, APCS Edition Chapter 10 Test Page 8 Updated: 05-16-13

Use the class and program segment below for questions 43-44. class MyList private int[] intarray; private int size; public MyList(int s) intarray = new int[size]; MyList list = new MyList(10); 43. Describes the result of executing the statement MyList list = new MyList(10); (B) (D) It instantiates a MyList object. It instantiates an int[] object called intarray It allocates spaces for ten int values. All of the above. 44. Which of the following describes the relationship of the objects? (B) (D) The program uses a MyList array object of int values. The program uses a MyList object of IntArray values. The program uses a MyList object with an int[] of int values. The program uses a IntArray object of MyList values. Exposure Java 2013, APCS Edition Chapter 10 Test Page 9 Updated: 05-16-13

Objective 9 - Two-Dimensional Arrays and length (This topic was also included in the earlier two-dimensional array section; this is a strict focus on length) 45. Consider the following two-dimensional array declaration. int[][] matrix = new int[4][4]; Which of the following statements will assign the correct size to rowsize? (B) (D) (E) int rowsize = matrix.length; int rowsize = matrix[0].length; int rowsize = matrix[1].length; int rowsize = matrix[2].length; All of the above 46. Consider the following two-dimensional array declaration. int[][] matrix = new int[4][4]; Which of the following statements will assign the correct size to colsize? (B) (D) (E) int colsize = matrix.length; int colsize = matrix[0].length; int colsize = matrix[1].length; int colsize = matrix[2].length; All of the above Exposure Java 2013, APCS Edition Chapter 10 Test Page 10 Updated: 05-16-13

47. Consider the following two-dimensional array declaration. int[][] matrix = new int[4][5]; Which of the following statements will assign the correct size to colsize? (B) (D) (E) int colsize = matrix[0].length; int colsize = matrix[1].length; int colsize = matrix[2].length; int colsize = matrix[3].length; All of the above 48. What will be printed by the following program statement? System.out.println(matrix[0].length); (B) (D) (E) The number of rows in a two-dimensional "square" static array. The number of columns in a two-dimensional "non-ragged" array. The number of columns in the top row of a two-dimensional static array. The number of columns in the row with index[0] of a two-dimensional array. All of the above Exposure Java 2013, APCS Edition Chapter 10 Test Page 11 Updated: 05-16-13

Objective 10 - Parameter Differences Between Simple Data Types and Arrays 49. Consider the following program. public class Question49 public static void main (String args[]) int p = 10; int q = 20; swap(p,q); System.out.println(p + " " + q); public static void swap(int x, int y) int temp = x; x = y; y = temp; What is printed as a result of executing the program? 10 20 (B) 20 10 10 10 (D) 20 20 (E) 0 0 Exposure Java 2013, APCS Edition Chapter 10 Test Page 12 Updated: 05-16-13

50. Consider the following program. public class Question50 public static void main (String args[]) int[] list = 1,2,3,4,5,6,7,8,9; swap(list,3,4); System.out.println(list[3] + " " + list[4]); public static void swap(int[] x, int p, int q) int temp = x[p]; x[p] = x[q]; x[q] = temp; What is printed as a result of executing the program? 3 4 (B) 4 3 4 5 (D) 5 4 (E) ArrayIndexOutOfboundsException Exposure Java 2013, APCS Edition Chapter 10 Test Page 13 Updated: 05-16-13

Exposure Java 2013, APCS Edition Chapter 10 Test Page 14 Updated: 05-16-13