100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
CPSC 1110 Final Exam Complete Questions And Answers $16.49   Add to cart

Exam (elaborations)

CPSC 1110 Final Exam Complete Questions And Answers

 2 views  0 purchase
  • Course
  • CPSC 1110
  • Institution
  • CPSC 1110

CPSC 1110 Final Exam Complete Questions And Answers

Preview 3 out of 30  pages

  • November 7, 2024
  • 30
  • 2024/2025
  • Exam (elaborations)
  • Questions & answers
  • CPSC 1110
  • CPSC 1110
avatar-seller
ElevatedExcellence
CPSC 1110 Final Exam Complete
Questions And Answers
Declare an array of integers containing the first five prime numbers CORRECT
ANSWERS int[] primes = { 2, 3, 5, 7, 11 };

Assume the array primes is {2, 3, 5, 6, 11} What does it contain after executing the
following loop?

for (int i = 0; i < 2; i++)
{
primes[4 - i] = primes[i];
} CORRECT ANSWERS 2, 3, 5, 3, 2

Assume the array primes is {2, 3, 5, 6, 11} What does it contain after executing the
following loop?

for (int i = 0; i < 5; i++)
{
primes[i]++;
} CORRECT ANSWERS 3, 4, 6, 8, 12

Given the declaration
int[] values = new int[10];
write statements to put the integer 10 into the elements of the array values with the
lowest and the highest valid index. CORRECT ANSWERS values[0] = 10;
values[values.length-1] = 10;

Declare an array called words that can hold ten elements of type String CORRECT
ANSWERS String[] words = new String[10];

Declare an array containing two strings, "Yes", and "No". CORRECT ANSWERS
String[] words = {"Yes", "No"};

Declare a method of a class Lottery that returns a combination of n numbers. You don't
need to implement the method. CORRECT ANSWERS public class Lottery
{
public int[] getCombination(int n) { . . . }
...
}

What does this enhanced for loop do?
int counter = 0;
for (double element : values)

,{
if (element == 0) { counter++; }
} CORRECT ANSWERS It counts how many elements have the value 0

Write an enhanced for loop that prints all elements in the array values CORRECT
ANSWERS for (double x : values)
{
System.out.println(x);
}

Why is the enhanced for loop not an appropriate shortcut for the following basic for
loop?
for (int i = 0; i < values.length; i++)
{
values[i] = i * i;
} CORRECT ANSWERS The loop writes a value into values[i]. The enhanced for loop
does not have the index variable i.

Given these inputs, what is the output of the LargestInArray program? 20 10 20 Q
CORRECT ANSWERS 20, 20

Write a loop that counts how many elements in an array are equal to zero. CORRECT
ANSWERS int count = 0;
for (double x : values)
{
if (x == 0) { count++; }
}

Consider the algorithm to find the largest element in an array. Why don't we initialize
largest and i with zero, like this?
double largest = 0;
for (int i = 0; i < values.length; i++)
{
if (values[i] > largest) { largest = values[i]; }
} CORRECT ANSWERS If all elements of values are negative, then the result is
incorrectly computed as 0.

What is wrong with these statements for printing an array with separators?
System.out.print(values[0]);
for (int i = 1; i < values.length; i++)
{
System.out.print(", " + values[i]);
} CORRECT ANSWERS If the array has no elements, then the program terminates with
an exception.

, When finding the position of a match, we used a while loop, not a for loop. What is
wrong with using this loop instead?
for (pos = 0; pos < values.length && !found; pos++)
{
if (values[pos] > 100) { found = true; }
} CORRECT ANSWERS If there is a match, then pos is incremented before the loop
exits.

When inserting an element into an array, we moved the elements with larger index
values, starting at the end of the array. Why is it wrong to start at the insertion location,
like this?
for (int i = pos; i < currentSize - 1; i++)
{
values[i + 1] = values[i];
} CORRECT ANSWERS This loops sets all elements to values[pos]

How can you print all positive values in an array, separated by commas? CORRECT
ANSWERS boolean first = true;
for (int i = 0; i < values.length; i++)
{
if (values[i] > 0)
{
if (first) { first = false; }
else { System.out.print(", "); }
System.out.print(values[i]);
}
}

Consider the following algorithm for collecting all matches in an array:
int matchesSize = 0;
for (int i = 0; i < values.length; i++)
{
if (values[i] fulfills the condition)
{
matches[matchesSize] = values[i];
matchesSize++;
}
} CORRECT ANSWERS Use the algorithm to collect all positive elements in an array,
then use the algorithm in Section 7.3.4 to print the array of matches.

Consider an 8 × 8 array for a board game:
int[][] board = new int[8][8];
Using two nested loops, initialize the board so that zeros and ones alternate, as on a
checkerboard:0 1 0 1 0 1 0 11 0 1 0 1 0 1 00 1 0 1 0 1 0 1. . .1 0 1 0 1 0 1 0 CORRECT
ANSWERS for (int i = 0; i < 8; i++)
{

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller ElevatedExcellence. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $16.49. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

77254 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$16.49
  • (0)
  Add to cart