XI C++ HOME ASSIGNMENT--SOLUTION (TEST C++ SKILLS)

CRACK TOUGH NUTS IN C++ Test your skills in C++ PART-1
“Useful for XI Computer Science Students to test their understanding lavel in C++ “
QUESTIONS WITH SOLUTION
1.Smallest Program in C++ : main(){}
2.Identify the erroes if any:
main(){ return 5;} // 0 error….Because “By default main() function returns an integer”.
int main(){
return 5;
} //no error
int main(){ return; // return <integral expression> Here function must return an integral value; }//or write void instead of int as return type of main() function.
void main(){
return;} // no error
char main(){
char c=65;
return c; } //error- main must return an int or void no other data type
int main(){
char c=65;
return c; }// No error..because c has its ASCII Code as its value and that is an integer
int main(){
return int(5.8999); }//0 error 0 warning
int main(){
return 5.8999; } // [warning] converting to “int” from “double”—loss of data due to truncation of fractional part
int main(){
return 5.8; }// [warning] converting to “int” from “double”
main(){
return 5.8; }// [warning] converting to “int” from “double”
main(){
char c=65;
return c;} // no error no warning //C Program To Display Hello Without Using Semicolon #include<stdio.h> #include<conio.h> main() { while(!printf("Hello World Without Using Semicolon")) { } if(getch()) { } }
//In C/C++ a semicolon is used to terminate a single statement.

But no need to terminate a block of statement i.e. { } // Conditional expression returning true/false or equivalent values in if,while,switch do not require a semicolon at end //So if we use the printf /cout/getch() statement inside these conditional // constructs/expression ,then we do not require any semicolon to terminate the statement. //C ++ Program To Display Hello Without Using Semicolon
#include<iostream.h> #include<conio.h> main() { if(cout<<"\n Hello World "){} //note : cout contains a zero/non zero/null/NOT NULL value that is //equivalent to true/false if(getch()){} //getch returns an integer value/ASCII code of the char entered it may be zero/non //zero..resulting into true/false }
#include<iostream.h>
#include<conio.h>
main() //find out the errors
{int _______________x=10,____________1111=20,1;
cout<< _______________x<<____________1111;
getch();} // no error ..we can use underscore in the beginning of variable name
// but avoid such names ..it may cause error(Multiple declaration error)..because name starting with //underscores are used by compilers / library //functions/header files for their internal use.
#include<iostream.h>
#include<conio.h>
main()
{ int y;
//comma operator use

y=(10,20,30,40);// equivalent to y=(40) or y=40;
//..value of the last expression /rightmost is the value of the whole expression in parenthesis..
cout<<y;
y=10,20,30,40; // equivalent to y=10; 20; 30; 40;
cout<<y;
int x =600;
y=(x/10+20,x*5+30,x-50/5); //equivalent to y= x-50/5..remaining get ignored..value of the last expression /rightmost is the value of the whole expression in parenthesis..
cout<<y;
y=x/10+20,x*5+30,x-50/5;// it is equivalent to 3 statemets
//1 // y=x/10+20; calculate/evaluate the expression and assign result to y
//2 // x*5+30 ; calculate/evaluate the expression and do nothing
//3 //x-50/5; calculate/evaluate the expression and do nothing
cout<<y;
getch();}
Output: 401059080 // I mean 40 10 590 80
Q6.Find the output and explain the ouput …
#include<iostream.h>
#include<conio.h>
void main() {
unsigned char i;
i=260; //0----- 255
cout<<int(i);
getch();} Answer: Output: 4

Because I will be having a value in the range 0…..255 it makes a cycle of values….256 is equivalent to 0 (first next value in the cycle after the completion/end/last value ), 257==1 , 258==2, 259==3, 260==4.
Q7.Find the output and explain the ouput …
#include<iostream.h>
#include<conio.h>
void main(){
signed char i;
i= -130;
cout<<int(i);
getch();} Answer: Output: 126 Because I will be having a value in the range -128…..127 it makes a cycle of values….-129 is equivalent to 127 (first next value in the cycle after the completion/end/last value ), -130==126 , -131==125, -132==124, -133==123…etc…..
Q8.Find the output and explain the ouput …
void main(){
short i;
i= -32770;
cout<<i;
getch();} Answer: Output: 32766 Because I will be having a value in the range -32768…..32767 it makes a cycle of values….-32769 is equivalent to 32767 (first next value in the cycle after the completion/end/last value ), -32770==32766 , --32771==32765 …etc…..
Q9.Find the output and explain the ouput …
#include<iostream.h>
#include<conio.h>
void main(){
int i=4;
cout<<"\n NOTE : << output operator inserts value from left to right to the cout object. \n"
" \n but...Expressions/values to be inserted for display are executed/evaluated from right to left direction \n\n";
cout <<"\n Final value of i = "<<i
<<"\n Value of cout<<++i<<i<<++i<<i++ = "<<++i<<i<<++i<<i++
<<"\n Starting value of i= "<<i;
getch();} Answer in Borland C++ 5.0 BC5 COMPILER : NOTE : << output operator inserts value from left to right to the cout object. but...Expressions/values to be inserted for display are executed/evaluated from right to left direction Final value of i = 7 Value of cout<<++i<<i<<++i<<i++ = 7664 Starting value of i= 4 Answer in DEV C++ 4.9.9.2 COMPILER  ( Expressions in cout are executed/evaluated from LEFT to RIGHT direction) NOTE : << output operator inserts value from left to right to the cout object. but...Expressions/values to be inserted for display are executed/evaluated from right to left direction Final value of i = 4 Value of cout<<++i<<i<<++i<<i++ = 5566 Starting value of i= 7 <<<<<<<<<<<<<<RESULT IS COMPILER DEPENDENT.. Try in other compilers also…>>>>>>>>>>>>>>
Q10.Find output and explain it also.
#include<iostream.h>
#include<conio.h>
void main(){
int i=4; int x=i++ + i++; // equiv to x=i+i; i++;i++; int y=++++++++i; // equiv to i++; i++; i++; i++; int z =++++i + ++++++i; //in DEV C++ it is equivalent to i++;i++;i++; i++; i++; z=i+i; / * in Bc++ it is equivalent to i=i+2; int temp1=i; i=i+3; int temp2=i; Z= temp1 + temp2; */ //BC5 compiler gets mad---:-) But DEV C++ compiler works fine ..Try other COMPILERS ALSO..
cout<<"\n x "<<x<<" y = "<<y<<" z "<<z<<" i ="<<i;
int a,b,c,d; a=i++ + i++; // eqiv to a=i+I; i=i+1; i=i+1; b=++i + ++i; //++ perform right to left--correct as per precedence rule //equiv to i=i+1; i=i+1; b=i+i; c=++i + i++;// equivalent to i=i+1; c=i+I; i=i+1; d=i + ++i;// Equivalent to i=i+1; d=i+I;
cout<<"\n a "<<a<<" b = "<<b<<" c "<<c<<" d "<<d<<" i ="<<i;
getch();} Answer in Borland C++ 5.0 BC5 COMPILER : x 8 y = 10 z 27 i =15 // why z=27   ?? no explanation..it is defective behaviour of Borland C++ a 30 b = 38 c 40 d 44 i =22 Answer in DEV C++ 4.9.9.2 COMPILER x 8 y = 10 z 30 i =15 // Z=30 CORRECT  a 30 b = 38 c 40 d 44 i =22 //BC5 compiler gets mad---:-) But DEV C++ compiler works fine ..Try other COMPILERS ALSO..
Note : For any query/suggestions/corrections/mistakes in the above solution feel free to write to hsyadav.kvs@gmail.com

Popular posts from this blog

XII- CS-- SOLVED SQL queries (SQL PRACTICAL ASSIGNMENS)

SQL--HOME ASSIGNMENTS(XII Class)

Python-MySQL Connectivity