Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answert he following question correctly. 1.Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array.

Please answert he following question correctly.

1.Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array. Which array algorithms might be adapted for this?

a) find the minimum

b) remove an element

c) add an element

d) calculate the sum of the elements

2.Which code snippet finds the largest value in an array that is only partially full?

a)

double largest = values[0]; 
for (int i = 1; i < values.length; i++) 
{ 
 if (values[i] > largest) 
 { 
 largest = values[i]; 
 } 
} 

b)

double largest = values[0]; 
for (int i = 1; i < values.length; i++) 
{ 
 if (values[i] < largest) 
 { 
 largest = values[i]; 
 } 
} 

c)

double largest = values[0]; 
for (int i = 1; i < currSize; i++) 
{ 
 if (values[i] > largest) 
 { 
 largest = values[i]; 
 } 
} 

d)

double largest = values[0]; 
for (int i = 1; i < currSize; i++) 
{ 
 if (values[i] < largest) 
 { 
 largest = values[i]; 
 } 
} 

3.Which code snippet calculates the sum of all the elements in even positions in an array?

a)

int sum = 0; 
for (int i = 1; i < values.length; i+=2) 
{ 
 sum++; 
} 

b)

int sum = 0; 
for (int i = 0; i < values.length; i++) 
{ 
 sum++; 
} 

c)

int sum = 0; 
for (int i = 0; i < values.length; i++) 
{ 
 sum += values[i]; 
} 

d)

int sum = 0; 
for (int i = 0; i < values.length; i = i + 2) 
{ 
 sum += values[i]; 
} 

4.If a programmer confuses the method required for checking the length of a string and uses size() instead of length(), what will happen?

a) The program will crash.

b) The program will not compile.

c) The program will run but will produce an uncertain result.

d) The compiler will automatically correct the error.

5. Which statements are true regarding the differences between arrays and array lists?

I. Arrays are better if the size of a collection will not change

II. Array lists are more efficient than arrays

III. Array lists are easier to use than arrays

a) I, II

b) I, III

c) II, III

d) I, II, III

6.The following statement gets an element from position 4 in an array:

x = a[4]; 

What is the equivalent operation using an array list?

a) x = a.get(4);

b) x = a.get();

c) x = a.get[4];

d) x = a[4];

7.Which code snippet calculates the sum of all the even elements in an array values?

a)

int sum = 0; 
for (int i = 0; i < values.length; i++) 
{ 
 if ((values[i] % 2) == 0) 
 { 
 sum += values[i]; 
 } 
} 

b)

int sum = 0; 
for (int i = 0; i < values.length; i++) 
{ 
 if ((values[i] % 2) == 0) 
 { 
 sum++; 
 } 
} 

c)

int sum = 0; 
for (int i = 0; i < values.length; i++) 
{ 
 if ((values[i] / 2) == 0) 
 { 
 sum += values[i]; 
 } 
} 

d)

int sum = 0; 
for (int i = 0; i < values.length; i++) 
{ 
 if ((values[i] / 2) == 0) 
 { 
 sum++; 
 } 
} 

8.Which one of the following code snippets accepts the integer input in an array list named num1 and stores the odd integers of num1 in another array list named oddnum?

a)

ArrayList num1 = new ArrayList(); 
ArrayList oddnum = new ArrayList(); 
int data; 
Scanner in = new Scanner(System.in); 
for (int i = 0; i < 10; i++) 
{ 
 data = in.nextInt(); 
 num1.add(data); 
 if (num1.get(i) % 2 == 0) 
 { 
 oddnum.add(num1.get(i)); 
 } 
} 

b)

ArrayList num1 = new ArrayList(); 
ArrayList oddnum = new ArrayList(); 
int data; 
Scanner in = new Scanner(System.in); 
for (int i = 0; i < 10; i++) 
{ 
 data = in.nextInt(); 
 num1.add(data); 
 if (num1.get(i) % 2 != 0) 
 { 
 oddnum.add(num1.get(i)); 
 } 
} 

c)

ArrayList num1 = new ArrayList(); 
ArrayList oddnum = new ArrayList(); 
int data; 
Scanner in = new Scanner(System.in); 
for (int i = 0; i < 10; i++) 
{ 
 data = in.nextInt(); 
 num1.add(data); 
 if (num1.get(i) % 2 == 0) 
 { 
 oddnum.add(num1[i]); 
 } 
} 

d)

ArrayList num1; 
ArrayList oddnum = new ArrayList(); 
int data; 
Scanner in = new Scanner(System.in); 
for (int i = 0; i < 10; i++) 
{ 
 data = in.nextInt(); 
 num1.add(data); 
 if (num1[i] % 2 != 0) 
 { 
 oddnum.add(num1[i]); 
 } 
} 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions

Question

3. What are potential solutions?

Answered: 1 week ago