Exam Preparation Tasks ( Errors Output)
Exam Preparation Tasks ( Errors Output)
Exam Preparation Tasks ( Errors Output)
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 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"?