module 6 zybook 7.2 Arrays Array declarations and accessing elements

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Can a programmer declare an array reference variable without allocating the array?

Yes.

What is the proper way to access the first element in array yearsArr? yearsArr[1] yearsArr[0]

yearsArr[0]

Assign tempVal with the myVals array element at the index one after the value held in variable i.

tempVal = myVals[i + 1];

allocate

to distribute

What is the index of the last element for the following array: int[] pricesArr = new int[100]; 99 100 101

99 The 100 elements will have indices 0..99.

What is array initialization in programming? A) The process of creating an array using the `new` keyword B) The process of setting the initial values of an array's elements C) The process of defining the data type of an array D) The process of accessing array elements through loops

: B) The process of setting the initial values of an array's elements

int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr [1] = 2012; yearsArr [2] = 2025; 1) How many elements in memory does the array declaration create?

4

What is the default value for elements of integer and floating-point data types in Java? A) 0 B) 1 C) true D) false

A) 0

What is the index of the first element in an array? A) 0 B) 1 C) -1 D) The size of the array

A) 0

What are the default values assigned to elements of integer and floating-point data types during array initialization? A) 0 for integer and floating-point, and false for boolean B) 1 for integer and floating-point, and true for boolean C) false for integer and floating-point, and 0 for boolean D) 0 for integer and floating-point, and true for boolean

A) 0 for integer and floating-point, and false for boolean

What does the following array initialization represent in Java? int[] numbers = {2, 4, 6, 8, 10}; A) An array of 5 integers initialized with the values 2, 4, 6, 8, and 10 B) An array of 5 integers initialized with default values C) An array of 5 integers with no initial values D) An empty array with no elements

A) An array of 5 integers initialized with the values 2, 4, 6, 8, and 10

What is an element in the context of an array? A) An ordered item in the array B) An unordered item in the array C) A data type used to define the array D) A variable used to access the array

A) An ordered item in the array

How can a programmer initialize an array with specific initial values without using the `new` keyword? A) By specifying the initial values in braces {} separated by commas B) By using the `new` keyword followed by specific values in parentheses C) By defining the data type of the array and then populating it with a loop D) By using the `set` method provided by the array class

A) By specifying the initial values in braces {} separated by commas

How do you access individual elements of an array? A) By using the array's name followed by the index in square brackets B) By using the array's name followed by parentheses C) By using the array's name followed by curly braces D) By using the array's name followed by a dot operator

A) By using the array's name followed by the index in square brackets

What is the purpose of an array declaration? A) To define the size and data type of the array B) To access the elements of the array C) To iterate through the array D) To modify the elements of the array

A) To define the size and data type of the array

Why do programmers use arrays? A) To maintain lists of items B) To store data of different data types C) To organize complex data structures D) To perform mathematical calculations

A) To maintain lists of items

What does the `new` keyword do in array creation? A) Assigns a value to an array element B) Allocates memory for the array C) Specifies the data type of the array D) Determines the size of the array

B) Allocates memory for the array

What is an array? A) A random list of items B) An ordered list of items of a given data type C) A data structure for storing different data types D) A collection of elements with no specific order

B) An ordered list of items of a given data type

What are [ ] symbols called in the context of arrays? A) Parentheses B) Brackets C) Braces D) Quotes

B) Brackets

How can a programmer initialize an array's elements with non-default values? A) By using the new keyword followed by specific values in parentheses B) By specifying the initial values in braces {} separated by commas C) By declaring the array without any values and then using a loop to assign them D) By using the set method provided by the array class

B) By specifying the initial values in braces {} separated by commas

What does the term "default value" refer to in programming? A) The value assigned to a variable after it has been modified B) The value assigned to a variable when it is created or declared without an explicit value C) The value that a variable must have before it can be used in a program D) The value assigned to a variable to mark it as empty or uninitialized

B) The value assigned to a variable when it is created or declared without an explicit value

What symbols are used in array declaration to indicate that the variable is an array reference? A) ( ) B) [ ] C) { } D) < >

B) [ ]

When does assigning an allocated array to reference variable occur? A) During the declaration of the array reference variable B) After the declaration of the array reference variable C) It is not possible to assign an allocated array to the array reference variable D) The assignment occurs automatically when an array is created

B) can be After the declaration of the array reference variable

In the example int[] myArray = {5, 7, 11};, how many elements are there in the array? A) 1 B) 2 C) 3 D) 4

C) 3

What are { } symbols called in the context of arrays? A) Parentheses B) Brackets C) Braces D) Quotes

C) Braces

What is the term for the number inside the brackets in an array access? A) Identifier B) Element C) Index D) Variable

C) Index

In the example `int[] gameScores = new int[4];`, what does `gameScores` refer to? A) The data type of the array B) The number of elements in the array C) The allocated array of four integers D) The index of the first array element

C) The allocated array of four integers

What happens to an array's elements when it is initialized using the new keyword? A) They are set to default values specified by the programmer B) They remain uninitialized and contain random values C) They are initialized to default values based on their data types D) The new keyword cannot be used to initialize arrays

C) They are initialized to default values based on their data types

What is the purpose of array declarations in programming? A) To specify the data type of the array B) To provide a name for the array C) To determine the number of elements the array can hold D) All of the above

D) All of the above

Recall that the array declaration was int [] yearsArr = new int [4];. Is currYear = yearsArr [4] a valid assignment? •Yes, it accesses the fourth element. No, yearsArr|4] does not exist.

No, yearsArr|4] does not exist. The valid indices start at 0, so the four elements are 0, 1, 2, and 3. Accessing yearsArr[4] will cause an error.

Declare and initialize an array named myVals that stores 10 elements of type int with default values. Assign variable x with the value stored at index 8 of array myVals. Assign the second element of array myVals with the value 555

int[] myVals = new int[10]; x = myVals[8]; myVals[1] = 555;

Assign myVals array element at the index held in currIndex with the value 777.

myVals[currIndex] = 777;


Kaugnay na mga set ng pag-aaral

apesWhich of the following best describes the conditions under which peat is formed?

View Set

Access Medicine Questions - Immunology

View Set

Pharmacology Module 4 - RESPIRATORY

View Set

Principles of economics ch. 33,34

View Set

Physiological Psych. Module 1.2 Quiz

View Set