100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten
logo-home
CPSC 1110 Final Exam Complete Questions And Answers $16.49
In winkelwagen

Tentamen (uitwerkingen)

CPSC 1110 Final Exam Complete Questions And Answers

 0 keer verkocht
  • Vak
  • CPSC 1110
  • Instelling
  • CPSC 1110

CPSC 1110 Final Exam Complete Questions And Answers

Voorbeeld 3 van de 30  pagina's

  • 7 november 2024
  • 30
  • 2024/2025
  • Tentamen (uitwerkingen)
  • Vragen en antwoorden
  • 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++)
{

Dit zijn jouw voordelen als je samenvattingen koopt bij Stuvia:

Bewezen kwaliteit door reviews

Bewezen kwaliteit door reviews

Studenten hebben al meer dan 850.000 samenvattingen beoordeeld. Zo weet jij zeker dat je de beste keuze maakt!

In een paar klikken geregeld

In een paar klikken geregeld

Geen gedoe — betaal gewoon eenmalig met iDeal, Bancontact of creditcard en je bent klaar. Geen abonnement nodig.

Focus op de essentie

Focus op de essentie

Studenten maken samenvattingen voor studenten. Dat betekent: actuele inhoud waar jij écht wat aan hebt. Geen overbodige details!

Veelgestelde vragen

Wat krijg ik als ik dit document koop?

Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.

Tevredenheidsgarantie: hoe werkt dat?

Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.

Van wie koop ik deze samenvatting?

Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper ElevatedExcellence. Stuvia faciliteert de betaling aan de verkoper.

Zit ik meteen vast aan een abonnement?

Nee, je koopt alleen deze samenvatting voor $16.49. Je zit daarna nergens aan vast.

Is Stuvia te vertrouwen?

4,6 sterren op Google & Trustpilot (+1000 reviews)

Afgelopen 30 dagen zijn er 69066 samenvattingen verkocht

Opgericht in 2010, al 15 jaar dé plek om samenvattingen te kopen

Start met verkopen
$16.49
  • (0)
In winkelwagen
Toegevoegd