CBSE eBooks

CBSE Guess > eBooks > Class XII > Computer Science By . Mr. MRK

Chapter – 1 C++ Revision Tour

Previous Index Next

1. f. In the following C++ program what is the expected value of MyMarks from options (i) to (iv)given below. Justify answer.2

#include<stdlib.h>
#include<iostream.h>
void main( )
{
randomize( );
int Marks[]={99,92,94,96,93,95},
MyMarks; MyMarks = Marks [1+random(2)];
cout<<MyMarks<<endl;
}
(i) 99 (ii) 94 (iii) 96 (iv) None of the above.

Ans: Output: (ii) 94

2007 Outside Delhi:

1.a. Differentiate between a Logical Error and Syntax Error. Also give suitable examples of each in C++. 2

Ans:

Logical Error: A logical error is that error which causes a program to produce incorrect or undesired output. An incorrectly implemented algorithm, or use of a variable before its initialization, or unmarked end for a loop, or wrong parameters passed are causes logical errors. These must be handled carefully.
For instance, if we are trying to print the table of a number 5 and if we say
counter=1;
while(counter>8)
{
cout<<n*counter;
counter=counter+1;
}
Here the loop would not be executed even once as the condition (counter>8) is not fulfilled at all. Therefore, no output will be produced. Such an error is logical error.

Syntax Error: Syntax errors occur when rules of a programming languages (syntax) is misused. Ie when a grammatical rule of C++ is violated.
Eg (i) c=a+b
In this statement, since there is no semicolon at the end of the statement, there will occurs a syntax error.
(ii)cin<<a;
In this statement, since stream insertion operator (<<) has given instead of stream extraction operation(>>), there will occurs a syntax error.

Previous Index Next