COMPUTER SCIENCE C++

e) Find the output of the following program.

#include<iostream.h>
void Withdef(int HisNum=30){ for(int I=20;I<=HisNum;I+=5)
cout<<I<<”,”;
cout<<endl;
}
void Control(int &MyNum)
{ MyNum+=10;
Withdef(MyNum);
}
void main()
{ int YourNum=20;
Control(YourNum);
Withdef();
cout<<”Number=”<<YourNum<<endl;
}

3

Ans: Output:

20,25,30,
20,25,30,
Number=30

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

#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.

2

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 parameterspassed 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 ofstream extraction operation(>>), there will occurs a syntax error.

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )