CSE 174 Arrays Content

Ace your homework & exams now with Quizwiz!

Which one of the following is the difference between the efficient version of the bubble sort and the normal one? a) It uses do-while instead of for loop. b) In worse case scenarios, it sorts the list faster than N-squared time c) The process of checking the list stops when the list is already sorted. d) It moves the maximum elements to the end of the list faster

a and c

Which of the following are true about all three sorting algorithms? a) The return type of their methods should be void b) They are faster than other sorting algorithms c) When a list is very scrambled they all sort the list in N-squared time d) They sort a list in place

a, c, and d

Compare the ArrayList vs Arrays and match the similar action: lst.add(10);

arr[0] = 10;

Compare the ArrayList vs Arrays and match the similar action: lst.set(15, 234);

arr[15] = 234;

Which one of the following only creates (not declaring) an array object? a)boolean[] arr = new boolean[10]; b) new char[10]; c) char[] arr; d) new char[];

b

What are the advantages of using ArrayLists over arrays? a) Nothing b) ArrayList is flexible with adding and removing elements. c) It is faster that array d) It's a class that uses arrays and provides methods for common actions.

b and d

Which of the following are correct for creating an array and initializing the array manually with some values? a) char[] chArr = new char[2]; b) int[] nums = new int[] {1, 2, 3}; c) double[3] nums = new {1.3, 2}; d) double[] nums = {1.4, 2, 3};

b and d

Which one of the following is the best way of describing an array? a) A sequence of values with different types b) none of the above c) A sequence of values all with the same type inside the memory d) A list of integers

c

Which one of the following only declares an array variable? (remember: declaring and creating are two different things) a) byte[10] arr; b) new byte[10]; c) byte[] arr = new byte[10]; d) byte[] arr;

d

ArrayList is efficient when there are lots of adding and removing calls.(True or false)

false

What is the result of the following code? boolean[] arr = new boolean[100]; System.out.println(arr[99]);

false

What value will be printed after running the following code: boolean[] b = new boolean[5]; double[] d = new double[5]; System.out.println(b[0] + " " + d[2]);

false 0.0

Assuming you have an array of doubles called grades. Fill in the blank so you can print the length of the array. System.out.println(_____)

grades.length

Compare the ArrayList vs Arrays and match the similar action: int a = lst.get(8);

int a = arr[8];

An ArrayList object can be printed directly, and the result would be the list of elements separated by comma enclosed in brackets.(True or false)

true

Foreach/enhanced loop can be used both for arrays and ArrayList objects.(True or false)

true

The bubble sort needs the maximum of N-1 passes to sort a list.(True or false)

true

Assuming in scanner object is already defined globally, and the display method prints the array elements on the display, what is the result of the following code if the user enters 3 values: 10 20 30 ? public static void main(String[] args) { double[] values = new double[3]; run(values); display(values); } public static void run(double[] lst) { for (double ch : lst) { ch = in.nextDouble(); } }

0.0 0.0 0.0

Using the binary algorithm, how many array elements would need to be looked at in order to locate the value 15? index: 0 1 2 3 4 5 6 7 8 value: 7 7 8 10 15 26 30 47 49

1

Using the linear search algorithm, how many array elements would need to be looked at in order to locate the value 14? index: 0 1 2 3 4 5 6 7 8 value: 14 2 95 14 6 8 0 75 13

1

What value will be printed after running the following code? ArrayList<Integer> lst = new ArrayList<Integer>(); lst.add(10); System.out.println(lst.size());

1

What would be the result of running the following code? String[] strs = new String[3]; strs[0] = "a"; strs[1] = "b"; for (int i = 0; i < strs.length; i++) { if (strs[i] != null) { System.out.print(strs[i].length() + " "); } }

1 1

Assuming in scanner object is already defined globally, and the display method prints the array elements on the display, what is the result of the following code if the user enters 3 values: 10 20 30 ? public static void main(String[] args) { double[] values = new double[3]; run(values); display(values); } public static void run(double[] lst) { for (int i = 0; i < lst.length; ++i) { lst[i] = in.nextDouble(); } }

10.0, 20.0, 30.0

Using the binary algorithm, how many array elements would need to be looked at in order to locate the value 7? index: 0 1 2 3 4 5 6 7 8 value: 7 7 8 10 15 26 30 47 49

2

What value will be printed after running the following code: ArrayList<Integer> lst = new ArrayList<Integer>(); for (int i = 0; i < 4; ++i) { lst.add(i + 1); } lst.set(3, 3); lst.set(1, 4); System.out.println(lst.indexOf(3) + " " + lst.indexOf(4) + " " + lst.indexOf(2));

2 1 -1

What is the result of the following code? boolean[] arr = new boolean[100]; int[] nums = new int[3]; if (!arr[0]) { System.out.println(nums[0] + 2.5); } else { System.out.println(arr[34]); }

2.5

Using the linear search algorithm, how many array elements would need to be looked at in order to locate the value 95? index: 0 1 2 3 4 5 6 7 8 value: 14 2 95 14 6 8 0 75 13

3

Assuming the linearSearch method is already defined, what is the result of running the following code? int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println(linearSearch(arr, 7));

6

What is the result of the following code snippet? public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 10, -20}; run(nums); System.out.println(nums[3]); } public static void run(int[] values) { values[values.length / 2] = values[4] - values[1]; }

8

Assuming you have a method that returns an array of ints. Fill in the blank with the right return type for the method. public static _____ check(int n)

int[]

What is the package of ArrayList class?

java.util

Assuming you have a method that accepts an array of long values called nums. Fill in the blank with the right parameter variable declaration. public static int check(_____)

long[] nums

Assuming the Car object is already imported, what value will be printed after running the following code: Car[] cars = new Car[3]; cars[0] = new Car("Toyota"); String[] s = new String[2]; s[0] = "Toyota"; System.out.println(s[1] + " " + cars[1]);

null null

Assuming the linearSearch method is already defined, what is the result of running the following code? int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println(linearSearch(arr, -2));

-1

What would be the result of following snippet code, assuming the bubbleSort method is already implemented correctly and display method prints the elements of the array? int[] arrOrigin = [9, 2, 4, -1, 5, 0, 7]; int[] copy = arrOrigin; bubbleSort(copy); display(arrOrigin); display(copy);

[-1, 0, 2, 4, 5, 7, 9] [-1, 0, 2, 4, 5, 7, 9]

What value will be printed after running the following code: ArrayList<Integer> lst = new ArrayList<Integer>(); for (int i = 0; i < 4; ++i) { lst.add(i + 1); } lst.add(3); lst.add(4); lst.remove(3); System.out.println(lst + " " + lst.size());

[1, 2, 3, 3, 4] 5

What value will be printed after running the following code: ArrayList<Integer> lst = new ArrayList<Integer>(); lst.add(201); ArrayList<Integer> temp = lst; temp.add(34); System.out.println(lst);

[201, 34]

Which one of the following declares and creates an ArrayList object called points with char type elements? a) ArrayList<Character> points = new ArrayList<Character>(); b) ArrayList<Char> points = new ArrayList<Char>(); c) ArrayList<Character> points = new ArrayList<Character>; d) ArrayList<Char> points = new ArrayList<Char>;

a

What happens with this code? public static double run(int[] grades) { return (grades[0] + grades[grades.length() - 1]) / 2.0; }

Compile error

After creating an array, it would be possible to change the size of the array directly. (True or False)

False

What would be the result of running the following code? String[] strs = {"hi", "hello", null}; System.out.println(strs[0].toUpperCase()); System.out.println(strs[2].toUpperCase());

HI runtime error: java.lang.NullPointerException

What would be the result of the following code? public static void main(String[] args) { int[] values = {1, 2, 3}; System.out.println(values[3]); }

Runtime error

Which one of the following does the same thing as line 2 of the following code? 1. for (int i = 0; i < 400; ++i) { 2. lst.add(i); 3. } a) lst.add(lst.size(), i); b) lst.add(lst.size() - 1, i); c) lst.set(lst.size(), i); d) lst.set(lst.size() - 1, i);

a

Fill in the blanks for printing the elements of the array by using the enhanced loop / foreach loop . public static void run(int[] grades) { for (_____value: _____) { //2 blanks System.out.println( _____); } }

int, grades, value

To check a list to see whether it's already sorted, the bubble sort and Insertion sort are better than the selection sort.(True or false)

true

When we define an array of objects, java uses null values as a default value for elements of the array.(True or false)

true

Using the linear search algorithm, how many array elements would need to be looked at in order to determine that the value 17 is not in the array? index: 0 1 2 3 4 5 6 7 8 value: 14 2 95 14 6 8 0 75 13

9

What does the following code do? public static void run(boolean[] grades) { for (int i = 0; i < grades.length; i++) { System.out.println(grades[i] + " "); } System.out.println(); }

Prints the element of the array

Fill in the blanks: public static int binarySearch(int[] arr, int key) { int low = 0; int high = _______; while (low <= high) { int mid = _______________; if (arr[mid] == key) { return ______; } else if (key > arr[mid]) { low = _______; } else if (key < arr[mid]) { high = ______; } } return ____; }

arr.length - 1, (low + high) / 2, mid, mid + 1, mid - 1, -1

Compare the ArrayList vs Arrays and match the similar action: lst.size();

arr.length;

Which of the following are correct about the enhanced (for-each) loop in java: a) Enhanced loop is very similar to while loop b) Enhanced loop is useful for reading the array elements. c) The value of the array elements cannot be changed inside the enhanced loop. d) There is no access to the index in enhanced loop.

b, c, and d

Which of the following are right about the selection sort? a) It's slightly better that bubble sort b) It has a simple code c) It always takes N-squared time to sort a list d) Brings the minimum element to the beginning of the list during each iteration e) It has too many writing to memory compared to Insertion sort f) It sorts the list in place, so no need to extra storage for sorting g) The process of moving the minimum element to the beginning of the list needs to be repeated N - 1 times

b, c, d, f, and g

Which of the following are true about the linear/sequential search: a) Only and only works with sorted arrays b) It's not very efficient compared to binary search c) Works with sorted arrays d) In worst case scenario it checks log2(n) times. e) In worst case scenario it checks n elements.. f) Works with unsorted arrays

b, c, e, and f

Assuming line 3 generates a random number between [1-500], which one of the following is true about the following code: ArrayList<Integer> lst = new ArrayList<Integer>(); for (int i = 0; i < 400; ++i) { int rnd = 1 + (int) (Math.random() * 500); if (!lst.contains(rnd)) { lst.add(rnd); } } a) It adds the numbers [0-399] to the list b) It only adds duplicate values to the list c) It only adds values to the list that are not already in the list d) Compile error because contains method returns an int value

c


Related study sets

Early Beliefs About the Origin of Life

View Set

Legal Environment of Business (MindTap) 8-11 & 23

View Set

Chapter 8- Wireless, Mobile Computing & Mobile Commerce

View Set

Ch. 1: About Strategy (Strategic Management 4e, Rothaermel) 2.0

View Set

AP Biology Unit 1: Chemistry of Life Test

View Set

Chapter 4: Linux File System Management (combined)

View Set

Responsibilities of Cybersecurity Roles

View Set

Marketing Research Exam #1 Study Guide/Questions

View Set

chapter 9 uppers downer and all arounders

View Set

Principles of Managerial Accounting - Chapter 16 Test

View Set

Experimental Probability Assignment

View Set

SS Guided Reading Chaper 6 Section 2

View Set

Spanish 1- 2. A COMENZAR: LAS PRIMERAS PALABRAS

View Set