Você está na página 1de 16

LOVELY PROFESSIONAL UNIVERSITY CLASS TEST - I: Object Oriented Programming Language SET-I (Group:--1) Test Solution Q1.

. Write program to find the factorial of a number entered by user A) Using recursion Ans1. void main() { clrscr(); int rect(int); int n,fact; Cout<<enter the no.; cin>>n; fact=rect(n); Cout<<factorial is<<fact; getch(); } int rect(int a) { int b; if(a==1) { return(1); } else { b=a*rect(a-1); return(b); } } Q2. WAP to compute row sum and column sum of a matrix. Ans2: #include <iostream.h> #include<conio.h> void main() {
1

int mat[10][10]; int i,j; int m,n; int sumrow,sumcol; cout<<\nenter the order of matrix; cin>>m>>n; cout<<\nenter elements of a matrix; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>mat[i][j]; } for(i=0;i<m;i++) { sumrow=0; for(j=0;j<n;j++) { sumrow=sumrow+mat[i][j]; cout<<"\n the sum of"<<i<<"row="<<sumrow; } for(j=0;j<n;j++) { sumcol=0; for(i=0;i<m;i++) { sumcol=sumcol+mat[i][j]; cout<<\nthe sum of"<<j<<"column="<<sumcol; } Q3. Through how many ways we can pass argument to a function and differentiate between all the argument passing techniques. Ans3: By 3 ways 1)Call by value 2)Call by reference 3)Call by address Explain these three along with the programs Q4. Give output for the following: void main() { int x=10,*y,**z,***v; y=&x;
2

z=&y; (**z)++; v=&z; cout<<*y,**z,***v;} Ans4:- 11 11 11

Give output for the following: void main() { char ptr2[]="c programming"; cout<<sizeof("c programming"); cout<<strlen("c programming") cout<<sizeof(ptr2); } Ans:-14 13 14 Q5. When you pass an array to a function, you are actually passing an address illustrate the concept with the help of program to find maximum element from an array Ans 5:- #include <iostream.h> void main() { int maximum(int [ ]); int a[5]; cout<<"Enter 5 numbers\n; for( int i = 0; i < 5; i++ ) { Cin>>a[i] ; } int max = maximum( a ); cout<<"\nMaximum value is ", max ; } int maximum( int b5] ) { int maxvalue = b[0]; for( int i = 0; i < 5; i ++)
3

{ if( b[i] > maxvalue ) { maxvalue = b[i]; } } return maxvalue; }

LOVELY PROFESSIONAL UNIVERSITY CLASS TEST - I: Object Oriented Programming Language SET-II
4

(Group:--1) Test Solution Q1. Create a logic for printing 3*3 matrix in the form that rows should be printing as columns and columns should be printed as rows. Ans1:#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10][10],m,n,i,j; cout<<"Enter number of rows: "; cin>>m; cout<<"Enter number of coloumns: "; cin>>n; if(m!=n) { cout<<"Matrix not square so Transpose not possible :("; } else { cout<<endl<<"Enter elements of matrix: "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<"Enter element a"<<i+1<<j+1<<": "; cin>>a[i][j]; } } cout<<endl<<"Displaying Matrix: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<endl<<endl; } cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++)
5

{ cout<<a[j][i]<<" "; } cout<<endl<<endl; } } getch(); } Q2. Illustrate the concept of constant pointer to pointer and generic pointer with the help of program Ans2:A constant pointer to a constant is: a pointer that points to a constant a pointer that cannot point to anything except what it is pointing to void main() { int i = 100; const int* const pi = &i; //*pi = 200; <- won't compile //pi++; <- won't compile } generic pointer When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to typecast it to another kind of pointer first. Hence the term Generic pointer. void main() { int i; char c; void *data; i = 6; c = 'a'; data = &i; cout<<"the_data points to the integer value << *(int*) data; data = &c; cout<<"the_data now points to the character << *(char*) data; }
6

Q3. Write program to generate Fibonacci series and print the location of any number entered by the user A) Using recursion Ans:#include <iostream.h> #include <conio.h> int y; fib(int n) { if(n==1 || n==0) return n; y=fib(n-1)+fib(n-2); return y; } Void main() { int a,r; clrscr(); cout<<"Enter any number : "; cin>>a; r=fib(a); cout<<"The no.<<a<<at position<<r; getch(); return 0; } Q4. Give output for the following: void main(){ int i; int *a,b=6; a=&b; for(i=0;i<4;i++) { Cout<<*a; *a=10; Cout<<*a; } }

Ans:6 10 10 10 10 10
7

10 10 b) Is there any error ?if yes then what and if no then what is the output void main() { char str[20]=c programming; const char * p=str; *p=y; puts(str); } Ans:- Pointer to constant is const char * p=str indicates that p is a pointer that points to a character array. character array is constant and value c cannot be changed y. However, the pointer p can be made to point to some other character array. Q5. A) which variable is accessible from within any block and which remains in existence for the entire execution of the program explain with example Ans:-External variable/Global variable Explain with example B)Which variable is accessed only within that function in which it is declared or defined explain with example Ans:-Local variable/Automatic variable Explain with example

LOVELY PROFESSIONAL UNIVERSITY CLASS TEST - I: Object Oriented Programming Language SET-I (Group:--2) Test Solution
8

Q1.If the solution of the sub problem applied repeatedly to solve the problem, in that case which concept of function is being used ? Explain the concept with the suitable example. Ans:-Recursion Example of factorial can be explained Q2. W.A.P to find the sum of elements of two matrix. Ans:#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10][10],b[10][10],c[10][10],m,n,i,j; cout<<"Enter number of rows: "; cin>>m; cout<<"Enter number of coloumns: "; cin>>n; cout<<endl<<"Enter elements of matrix A: "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<"Enter element a"<<i+1<<j+1<<": "; cin>>a[i][j]; } } cout<<endl<<"Enter elements of matrix B: "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<"Enter element b"<<i+1<<j+1<<": "; cin>>b[i][j]; } } cout<<endl<<"Displaying Matrix A: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; }
9

cout<<endl<<endl; } cout<<endl<<"Displaying Matrix B: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<b[i][j]<<" "; } cout<<endl<<endl; } cout<<endl<<"Matrix A + Matrix B = Matrix C: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<a[i][j]+b[i][j]<<" "; } cout<<endl<<endl; } getch(); } Q3. Is there any difference b/w null pointer and void pointer? If yes explain the difference with the help of example Ans: NULL value can be assigned to any pointer, no matter what its type. void *p = NULL; int i = 2; int *ip = &i; p = ip; cout<<*p; cout<<*((int*)p ) ; generic pointer When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to typecast it to another kind of pointer first. Hence the term Generic pointer. void main() { int i; char c;
10

void *data; i = 6; c = 'a'; data = &i; cout<<"the_data points to the integer value << *(int*) data; data = &c; cout<<"the_data now points to the character << *(char*) data; } Q4. Give output for the following: void main() { int x=10,*y,**z,***v; y=&x; z=&y; (**z)--; v=&z; cout<<*y<<**z<<***v;} Ans:-9 9 9 Is there any error ?if yes then what and if no then what is the output void main() { char str[20]=c programming; const char * p=str; *p=y; puts(str); } Ans:- Pointer to constant is const char * p=str indicates that p is a pointer that points to a character array. character array is constant and value c cannot be changed y. However, the pointer p can be made to point to some other character array.

Q5. Define the following: (a)Break and continue. (b)Inheritance (d)Reference and dereference operator. (e)Actual and formal arguments.

11

Ans:a) break statement Causes immediate exit from the loop Used in while, for, dowhile or switch statements void main ( ) { int k; for ( k= -5; k < 25; k= k+5) { cout<<k; break; cout<< Good Morning << endl; } } Output = -5 continue statement Skips remaining statements in loop body and start from the beginning of loop Used in for loop, while , dowhile loops4 Void main ( ) { int k; for ( k= -5; k < 25; k= k+5) { cout<<k; conitnue; cout<< Good Morning << endl; } } Output = - 5 0 5 10 15 20 b) Inheritance is a feature of oops that Deriving a new class called as derived from existing base class c)Reference operator is address operator
12

Deference operator is used to display value stored at that address int a; int*p p=&a; Cout<<*p; Cout<<p; Cout<<&a; d) Arguments of calling function is actual Arguments of called function is formal Explain with example

LOVELY PROFESSIONAL UNIVERSITY CLASS TEST - I: Object Oriented Programming Language SET-II (Group:--2) Test Solution Q1. Is there any difference between constant pointer and pointer to constant? If yes then justify the statement with the suitable example
13

ANS:A constant pointer, ptr, is a pointer that is initialized with an address, and cannot point to anything else. We can use ptr to change the contents of value Example int value = 22; int * const ptr = &value; Pointer to constant is:const int * ptr1 indicates that ptr1 is a pointer that points to a constant integer. The integer is constant and cannot be changed. However, the pointer ptr1 can be made to point to some other integer. int i = 100; const int* pi = &i; //*pi = 200; <- won't compile pi++; Q2. Is there any error ?if yes then what and if no then what is the output void main() { int i = 100; const int* const pi = &i; *pi = 200; pi++; } Ans:- pointers type is constant pointer to constant so address and value cant be changed. explain the concept of constant pointer to constant b)Is there any error ?if yes then what and if no then what is the output void main(){ int i = 100; int* const pi = &i; *pi = 200; Cout<<*pi;} Ans:- 200 Q3. Differentiate between call by value and call by reference only with the help of example Ans:Call by value
14

void main() { int a=10,b=20; void swap(int,int); Cout<<before function calling<<a<<b; swap(a,b); Cout<<after function calling<<a<<b; getch(); } void swap(int x,int y) { int z; z=x; x=y; y=z; Cout<<value is<<x<<y; } Call by reference #include<iostream.h> void swap(int &, int &); void main() { int iVar1, iVar2; cout<<"Enter two numbers ; cin>>iVar1; cin>>iVar2; swap(iVar1, iVar2); cout<<"In main "<<iVar1<<iVar2; } void swap(int &iNum1, int &iNum2) { int iTemp; iTemp = iNum1; iNum1 = iNum2; iNum2 = iTemp; cout<<"In swap "<<iNum1<<" "<<iNum2<<endl; } Q4.W.A.P to find the sum of elements of two matrix Ans:-see page 8
Q5. A) Which storage class can be used only if we want the value of a variable to persist between different function calls . Ans:-Static storage class

B) Which type of variable is used to increases the resultant speed of operations


Ans:-register variable 15

c) Which variable is accessible from within any block and which remains in existence for the
entire execution of the program
Ans:-external/global D) Which variable is accessed only within that function in which it is declared or defined Ans:-loacal/automatic

16

Você também pode gostar