Вы находитесь на странице: 1из 8

International Indian School, Dammam Computer Science Dept.(GSS) .

Worksheet (C++ programming)



~

,

1. Describe the two ways to include comments in C++program.

2. What is cascading of f/O operators?

3. What are C++ tokens?

4. What are manipulators? Name 2 manipulators and their use.

5. What is cin and cout? Mention their uses.

6. What-is setw() ?give examples. .

7. What is a header file? Write the importance of including it in C++ program?

8. What are the data items?

9. What is declaration of variables? G,tvt.. ~ wOf D.h ckth~ v·w.ltJdu -

10. What is the use of the maim) function in C++ programming? Give two reasons.

11. What is processor directive? Whatis its role?

12. What are character and string constant?

13. Explain back slash char constants with examples.

14. List the rules for naming the identifier.

15. What is const modifier? What is the difference between const variable and normal

variable?

16. Differentiate between signed and unsigned data types.'

17. What is e++ shorthand operators?

18. Differentiate between while and do .. while?

19. Explain for-loop with an example.

20. What is nested for loop?

21. What is the use of exitt) function? How is it different from break statement?

22. What is the use of switch -default statement?

23. What is the difference between the binary and unary operators?

24. Write the advantages of using #include compiler directive?

25. What is fundamental data type? Give examples.

26. What is integer constant?

27. Differentiate variable declaration and variable initialization.

28. What is type casting? Explain implicit and explicit type conversion with

examples.

29. Explain the nested if statement.

30. What is the difference between else . .ifladder and switch statement?

31. What is the significance of break in switch statement?

32. What is comma operator? Explain with examples.

33. What is conditional operator?

34. Write the header files for the following: exitj), powO, endl, setwj), clrscrt)

35. Write one advantage and one limitation of switch statement. '., ~

sc, ~L.\ L; 1;1f'L ~I><ih''''' ~ ~.Ai. tliM/.,_g",u- ~ ~ r

3f t2rcto ~ ~ ~ . MWc rrc- ~clW f; =r .

". ~~ j,.o.. 1fu., ~U)W\Jl.. ~ ~ (_ ).M ~.A/'"

,39- vvl~ ,U. :J

Z,9 ~~~t~~'~' M.~.' ~ ~" ~!"""S'~.; ito . ~ aAL ~. F

International Indian Scholl, Damrnam Computer Science Dept. (OSS) -Worksheet (Topic: Arrays)

1. How is getsO different from cint)?

2. What is an array?

3. What is meant by the index of an array? How are indices numbered in C++?

4. Why array is called a derived data type?

5. Can an array be assigned to another of the same size and type? For example if two arrays A[S] and B[S] are available of same type, what would be the effect of the following statement? A=B.

6. Identify the error: (i) ch=getchar; (ii) gets("name");

7. If the string input is "My File Name", what is the content of name if the following input statements are given?

(i) getsmame);

(ii) cin»name;

8. What will be the output: (a) void mainO

{

char S[]= "school"; int I = strlen(S)~ for(int c=O;c<l;c++) i£t islower(S [c]))

S[ c ]=toupper(S[ c]); else if( c%~ 0) S[c]='E'~

else S[c]=tolower(S[c]); cout-c "New Message"«S;}

(b) void MainO

{

int b=O;

int clIO] = {1,2,3,4,5,6,7,8,9,0}; for(int a=O;a<1 O;a++)

{

if(c[a]%2= =0) b+=c[a];

}

cout«''b=''«b~

}

..



(c) void maint) {

int

A[ 1 O]={O,l ,2,3,4,5,6,7,8,9} ~ for(int i=8~i>=0;i-)

A[i+ 1 ]=A[i]~

fon i=Oii-c 1 O~i++) cout«" "«A[i]~

}

. (d) void mainf) {

char a[]= "c+rprogram"; for(int i=10;i>=0;i--)

{

fortint j=O;j<i;j++) cout<<a(j]; cout-ccendl:

P-

o J

(e) fortint i=I; i<IO;i++) if(i= =4)

cout-c-ci;

(f) void maint) {

int a[5],1; for(i=O;i<5~i++ ) a[i] = 5*1; for(i=0;i<5;i++ ) cout<<a[i];

}

(g) void maint) {

int a, b=O;

int c[10] = {1,2,3,4,5,6,7,8,9,10}; fore a=O;a<1 O~a++)

if(a%2= =0) b+=c[a]; cout-ccb;

}





(h) fonint i=I;I<=3;i++) fonint j=1;j<=4;j++) cout«'\n"«i+j;

(i) int a[6]={ 6,5,4,3,2,1}; int i; forCi=5;i>=0~i++ ) cout<<a[i];

(j) void mainO {

int a[5]={5,10,15,20,25}; int i=O,j,k-3;

int m;

i=++a[l];

j=a[2]++~

m=++a[++k];

cout«i«endl<<j<<endl<<m<<endl; forCi=O;i<5;i++ ) cout«a[i]«" ";

}

• •

Class XI Worksheet

International Indian School, Dammam Department of Computer Science, GSS

Topic: Flow of control (finding outputs)

Assuming #include<iostream.h> has been included, find the output of the following:

Q 1 void maint) {

int i = 4; while(i++>4) cout-c-ci;

} II no olp

Q2

void maim)

{

int i = 4; while( ++i>4) cout-c-ci;

} I linfinite loop 5 6 7 8 ....

Q3

void maint) {

int i = 4; while( ++i>4) { i-=2; cout'<-ci;

}

} olp 3

Q4

void maim)

{

int i = 4; while( ++i>4) {

cout-c-ci; i-=2;

}

} olp 5

Q5

void maim)

{

int k=2; switch(k++ )

{

case 2 :cout«"\nthis is case 2 and k='<xk; break;

case 3 :cout«"\nThis is case 3 and k="«k; break;

case 4 : cout «"\nThis is case 4 and k="«k; break;

default: cout«"\nDefault stm being executed";

}

} o/p This is case 2 and k=3

Q6

void maim)

{

int k=2; switch(++k)

{

case 2 :cout«"\nthis is case 2 and k='<ck; break;

case 3 :cout«"\nThis is case 3 and k="«k; break;

case 4 : cout «"\nThis is case 4 and k=I'<xk; break;

default: cout«"\nDefault stm being executed";

}

} o/p This is case 3 and k=3

Q7

void maint) {

int k=2; switch(++k)

{

case 2 :cout«"\nthis is case 2 and k="«k; break;

case 3 :cout«"\nThis is case 3 and k="«k; k++;

break;

case 4 : cout «"\nThis is case 4 and k="«k; break;

default: cout«"\nDefault stm being executed";

}

} o/p This is case 3 and k=3

Q8

void maim)

{

int i=3;

if (i++==4) cout< <"\nfour"; else if (++i == 5) cout«"\nfive";

2-

else

cout< <"\nsix"; } o/p is five

Q9

void maint) {

int i=3;

if (i++==4) cout< <"i="< <i; else if (++i == 5) cout«"i="«i; else cout«"i="«i; } o/p is i = 5

QI0

void maim)

{

int i=3;

if (i++==4) cout< <"\nfour"; else if (i++ == 5) cout< <"\nfi ve"; else

cout< <"\nsix"; } o/p is six

Qll

void maim) {

int i,n=4;

for (i=O;i<n;i++) cout«i«",";

} o/p is 0,1,2,3

Q12

void maim)

{

clrscn); int i,n=4;

for (i=O;i++<n;i++) cout-c-ci'<-c'',";

} o/p is 1,3,

Q13

void maim) {

int i,n=5;

for (i=O;i++<n;i++) cout-c-ci'<-c'',";

3

} olp is 1,3,5,

Q14

void maim) {

int i,j;

for (i=Oj=4;i<j;i++,j--) cout«i«","<<j<<"\n";

} olp is 0,4

1,3

QI5

void maim)

{

int ij;

for (i=Oj=4;i<=j ;i++ j--) cout«i«","<<j<<"\n"; } olp is 0,4

1,3

2,2

Theory questions:

1. What is a loop? Explain with an example.

2. Differentiate between entry controlled loop and exit controlled loop.

3. Distinguish the while and do .. while loops.

4. How does a switch statement differ from a if else? Explain with an example.

5. What is the use of a default statement in switch? Explain with an example.

6. Illustrate with an example the use of break statement in switch.

7. What is a nested loop?

8. Give the syntax ofthe for, while and do .. while statements.

9. Illustrate the working of an entry controlled loop.

10. Explain with an example the exit controlled loop. Why is it called so?

International Indian School, Dammam 'Computer dept. -GSS '

. Worksheet- 2

1. Assuming that integer type variable "val"starts with value 99. What will be the following code fragment print out? coutccval-;

eout«++val;

2. What will be the output of the _ following?, ,#inClWie<iostre3:m.h> voidlIlairi() , '

{

int a=S,b=3,x; float y; x=a%b;

y = a/(float)b; eout<<x<<''\n''<<y;

}

3. Evaluate intz,x=3,y=2; z= --x +y++; cout-ccz:

4. 'Evaluate if a::7,b=3,c=S,d=6 (i). !(a<b)&&e!=dUb+c<a

(ii). (b+a>d+4)1Ia>20)&&! (e)1I (c<d)&&( c+b l=a+ 1)

5. Guess the output: #include<iostream.h> voidmainO

{

int x = 5, y=5; cout«x--; cout«"," ; cout« --x; cout-cc",";

1 O. What will be the o/p #include<iostream.h> void mainO

{ intn=12;

cout«n+t« -n« ++n« n-;

}

11. Guess the o/p. #include<iostream.h> YoidmainO

{

int m = 35;

'(m!=(m = m++»?cout<~"true":cout«"false";

}

,.'

cout«y-«","« -y;

}

6. what will be the output? #include<iostream.h> voidmainO

{

float val; , ,

val = (float)2+1SI(float)4; cout«'~val="<<val'

,'., ,

}

7. What \yiit be the output? #include<iostream.h> voidmainO

{ , ,

float k =22; k=++k==23; cout-oek;

}

8., Guess the o/p_ #include<iostream.h>

. void mainO {

intj = ~34>=~4~ cout-ccl;

}

9. Guess the o/p. #include<iostream.h> voidmainO

{

int i = 125; i = i= = 125;, coutcci;

}

12. Guess the o/p. #include<iostream.h> voidmainO

{

int m = 15,n=14; (m!=(n=n++»?cout«"true":cout«"false";

}