Exam Preparation Tasks ( Errors Output)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Course Title: Programming Fundamentals Batch: 2024F

Practice Tasks
Task 1: Predict the output of the following program.
#include <iostream>
int main()
{
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int i = 0; i < 3; i++)
{
count++;
}
}
Cout<< count;
}

Task 2: What is the output when the following code fragment is executed?
int main ()
{
int n;
for (n = 5; n > 0; n--)
{
printf("%d", n);
if (n == 2)
break;
}
return 0;
}
Task 3: Identify the logical error in following code fragment.
int main() {
int arr[] = {10, 20, 30};
cout << arr[3] << endl;
return 0;
}

Task 4: Identify the logical error in following code fragment.


void allocate() {
int* arr = new int[100];
}
int main() {
allocate();
return 0;
}
Course Title: Programming Fundamentals Batch: 2024F

Task 5: What is the output when the following code fragment is executed?
int main() {
int n = 5;
do {
cout << n << " ";
} while (n > 0);
return 0;
}

Task 6: What would be the output when the following code fragment is executed?
int n, k = 5;
n = (100 % k ? k + 1 : k - 1);
cout << "n = " << n << " k = " << k << endl;

Task 7: What would be the output when the following code fragment is executed?
int main() {
int a = 100, b = 200;
int *p = &a, *q = &b;
if(*p >= *q)
cout<<*p+5;
else
cout<<*q–10;
return 0;
}

Task 8: The nested conditional statement shown below has been written by an inexperienced C/C++
programmer. The behavior of the statement is not correctly represented by the formatting.
if (n < 10) if (n > 0)
cout << "The number is positive." << endl; else
cout << "The number is ." << endl;
What is the output of the statement if the variable n has the value 7? If n has the value 15? If n
has the value -3?
Task 9: What will be the exact output of nested loop after it is executed?
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j)
continue;
cout << i * j << "\t";
}
cout<<"\n";
}
return 0;
}
Course Title: Programming Fundamentals Batch: 2024F

Task 10: Answer the questions below concerning the following fragment of code.
main()
{
int n;
cout<<"Enter an integer: ";
cin>>n;
if (n < 10) {
cout<<"less than 10\n";
} else if (n > 5) {
Cout<<"greater than 5\n";
} else {
cout<<"not interesting\n";
}
}
What will be the output of the fragment above if the interactive user enters the integer values 0, 15 and 7?
Also, what values for n will cause the output of the fragment above to be "not interesting"?

Task 11: What is the exact output of the program below?


int main() {
int n = 3;
while (n >= 0) {
cout<<n * n;
--n;
}
Cout<<n;
while (n < 4) {
++n;
Cout<<n;
}
Cout<<n;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy