[Solved] CSCE 121 Lab 5

$25

File Name: CSCE_121_Lab_5.zip
File Size: 131.88 KB

SKU: [Solved] CSCE 121 Lab 5 Category: Tag:
5/5 - (1 vote)

This lab differs from the previous ones as there are no new programming exercises. It is intended to give you an opportunity to go back over the previous weeks lab exercises and to complete those questions you were unable to finish previously. This may include questions where you have an answer, but for which you harbor suspicions that your solution only works in particular limited cases or might be improved in some way.

If youve found the questions to be easy so far, then use this lab to write radically new programs as answers to the most interesting of the questions. For example, if you used a loop in your previous answer, try to do it with a recursive function call instead.

So what exactly is this lab?

Your lab time is best spent getting help on the trickiest questions in the previous labs:

Here are some questions that are intended as practice questions for the quizzes and midterm exams. Try to do them without using a computer or any external help, but then carefully check your answers using g++.

Question 1

#include <iostream>using namespace std; void func-x(int p[4]){ int i=10; p=&i; cout << p[0]} int main(){ int arr[4] = {1, 2, 3, 4} func-x(arr); cout << arr[0]; return 0;}

What will be the output of the above program?

  1. 10 10
  1. Compiler Error
  2. 10 1
  3. Undefined Behavior
  4. 1 10

Question 2

#include <iostream>using namespace std; int main(){ int arr[4] = {1, 2, 3, 4}; int p[4]; p=arr; cout << p[1];}

What will be the output?

  1. 1
  1. Compile time error
  2. Undefined Behavior
  3. 2
  4. {2, 3, 4}

Question 3

Which of the following is an illegal declaration?

  1. int a=0, b = 1, c=2; int array[3] = {a, b, c}
  1. int size = 3; int array[size];
  2. int size = 3; int array[size] = {1, 2, 3};
  3. int array[3] = {1, 2, 3, 4};
  4. All of the above

Question 4

#include <iostream>using namespace std; int main(){ int arr[5] = {1,2,3,4,5}; int i = -1; cout << arr[5]; return 0;}

What will be the output of the above program?

  1. Garbage/undefined value
  1. 5
  2. 6
  3. 0
  4. None of the above

Question 5

#include <iostream>using namespace std; int main(){ int a[5] = {51, 1, 5, 20, 25}; int x, y, z; x = ++a[1]; y = a[1]++; z = a[x++]; cout << x << << y << << z; return 0;}

What will be the output of the above program?

  1. 2, 3, 20
  1. 2, 1, 5
  2. 2, 2, 5
  3. 1, 2, 5
  4. 3, 2, 5

Question 6

An arrays elements are always stored in ________ memory locations.

  1. Sequential
  1. Random
  2. Sequential and Random
  3. Sometimes sequential and sometimes random
  4. None of the above

Question 7

#include <iostream>using namespace std; int main(){ char str[5] = XYZ; cout << str[3]; return 0;}

What will be the output of the above program?

  1. XYZ
  1. Nothing will be printed
  2. Z
  3. 0

Question 8

#include <iostream>using namespace std; int main(){ int array[] = {10, 20, 40, 50}; cout << 3[array]; return 0;}

What will be output?

  1. 40
  1. Compilation Error
  2. 30
  3. 50
  4. 150

Question 9

#include <iostream>using namespace std; int main(){ int i,j,k; for (i=1; i<=5; i++) { for (j=5; j>=i; j) { cout << ; } for (k=1; k<=i; k++) { cout << *; } cout << endl; } return 0;}

What is output?

  1. * * *c. * * *d. * * * *
  2. * * * * *
  3. * * * h. * * * i. * * * *j. * * * * *
  4. * * *n. * * *o. * * * *p. * * * * *
  5. * * *t. * * *u. * * * *v. * * * * *w. * * * *x. * * *y. * *z. *
  6. * * * *dd. * * * * *ee. * * * * * * *ff. * * * * * * * * *gg. * * * * * * * * * * *

Question 10

#include <iostream>using namespace std; void modify(int &num1, int num2){ num1 += 2; num2 += 1;} int main(){ int A = 20, B = 4; modify(A, B); cout << A << , << B << endl; modify(B, A); cout << A << , << B << endl; return 0;}

What is output?

  1. 22, 424, 5
  1. 20, 420, 4
  2. 22, 422, 6
  3. 20, 420, 4
  4. 22, 524, 6

Question 11

#include <iostream>using namespace std; void order(float my_list[], const int size){ float next_min, temp; int location; for(int i=0;i<size;i++) { next_min = my_list[i]; location = i: for(/* complete me */) { if(my_list[j] < next_min) { next_min = my_list[j]; location = j; } } //Swap the values temp = my_list[i]; my_list[i] = my_list[location]; my_list[location] = temp; }} int main(){ const int size = 5; float my_list[size] = {2.3f, 0.0f, 1.2f, -9.3f, 8.4f}; order(my_list, size); for(int i=0;i<size;i++) { cout << my_list[i] << ; }}

Fill in the incomplete for loop so that at the jth index, the value at j and the smallest value of the remaining list (j+1 to end) are swapped.

This method of sorting is known as the selection sort.

Question 12

#include <iostream>using namespace std; int main(){ int a[7] = {1, 2, 3, 4, 5, 6, 7}; int sum; int i; for (i=0; i<7; i++) { sum += a[i]; } cout << sum << endl; return 0;}

What is the output?

  1. 28
  1. 21
  2. Segmentation Fault
  3. Compiler Error
  4. None of the above

Question 13

#include <iostream>using namespace std; int main(){ for (int i=0; i<3; i++) { for (int i=0; i<3; i++) { cout << i << ; } } return 0;}

What is the output?

  1. Segmentation Fault
  1. Compiler Error
  2. 0 1 2 0 1 2 0 1 2
  3. 0 1 2 3 4 5 6 7 8
  4. 0 1 2 3 4 5 6 7 8 9

Question 14

#include <iostream>using namespace std; int main(){ for (int i=0; i<3; i++) { int number = i; while (number < 3) { number++; } } cout << number << endl; return 0;}

What is the output?

  1. 9
  1. 3
  2. 6
  3. Segmentation Fault
  4. Compiler Error

Question 15

#include <iostream>using namespace std; int main(){ float f = 0.7; if (f == 0.7) cout << Yes << endl; else cout << No << endl; return 0;}

Whats the output?

  1. Yes
  1. No
  2. Nothing (No output given)
  3. Segmentation Fault
  4. Compiler Error

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCE 121 Lab 5
$25