Starting from:

$30

CSCE121-Lab 5 Solved

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?


10 10
Compiler Error
10 1
Undefined Behavior
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
Compile time error
Undefined Behavior
2
{2, 3, 4}
 

Question 3
Which of the following is an illegal declaration?

int a=0, b = 1, c=2; int array[3] = {a, b, c}
int size = 3; int array[size];
int size = 3; int array[size] = {1, 2, 3};
int array[3] = {1, 2, 3, 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?

Garbage/undefined value
5
6
0
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?

2, 3, 20
2, 1, 5
2, 2, 5
1, 2, 5
3, 2, 5
Question 6
An array's elements are always stored in ________ memory locations.

Sequential
Random
Sequential and Random
Sometimes sequential and sometimes random
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?

XYZ
Nothing will be printed
Z
0
\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?

40
Compilation Error
30
50
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?

*
* *
* * *
* * * *
* * * * *
*
* *
* * *
* * * *
* * * * *

*
* *
* * *
* * * *
* * * * *

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *

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?

22, 4
24, 5
 
20, 4
20, 4
 
22, 4
22, 6
 
20, 4
20, 4
 
22, 5
24, 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?

28
21
Segmentation Fault
Compiler Error
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?

Segmentation Fault
Compiler Error
0 1 2 0 1 2 0 1 2
0 1 2 3 4 5 6 7 8
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?

9
3
6
Segmentation Fault
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;
}
What's the output?

Yes
No
Nothing (No output given)
Segmentation Fault
Compiler Error

More products