0% found this document useful (0 votes)
47 views11 pages

OOP Sessional 1 Solution

The code defines a struct called someStruct with fields x, y, z, and ptr. It initializes an instance called s1 with values. It allocates memory for s1.z and assigns string values. It initializes s1.ptr and allocates/assigns for s1.ptr->z. It sets s1.ptr->ptr to point back to s1. A do-while loop prints the last element of s->z, sets s to s->ptr, and repeats until s equals the original s1 pointer. This outputs the two string values "**ur50c001".

Uploaded by

Muhammad Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views11 pages

OOP Sessional 1 Solution

The code defines a struct called someStruct with fields x, y, z, and ptr. It initializes an instance called s1 with values. It allocates memory for s1.z and assigns string values. It initializes s1.ptr and allocates/assigns for s1.ptr->z. It sets s1.ptr->ptr to point back to s1. A do-while loop prints the last element of s->z, sets s to s->ptr, and repeats until s equals the original s1 pointer. This outputs the two string values "**ur50c001".

Uploaded by

Muhammad Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Question 1 [6 + 4+ 2.5 + 2.

5=15 Marks]

What will be the output of the given programs? In case of an error(s), circle that part in the code and
mention the reason for that error(s) in one line.

S no. Question Output

1.1 #include <iostream> 2


using namespace std;
int a = 9; 9
int b = 2; 1
int *p = &a;
1
int* func1(){
return &b;
}
int* func2 (int* p){
return p;
}
int& func2(){
return *p;
}
int& func3(){
return a;
}
int main() {
int a = 4;
int* p ;
cout << *(func1()) << endl;

p = func2(&::a);
cout << *p << endl;

func2() = 1;
cout << ::a << endl;

a= func3();
cout << a <<endl;

return 0;
}
1.2. #include <iostream> 1. bject Oriented Programming
using namespace std; 2. P
int main(){ 3. e b
char oop[] = "Object Oriented 4. t Oriented Programming
Programming";
char* pointer = oop;
cout << "1. " <<++pointer <<endl;
cout << "2. " << ++(*oop) <<endl;
cout << "3. " << pointer[2] << " "
<< *pointer <<endl;
cout << "4. " << oop+5 <<endl;
return 0;
}

1.3. #include <iostream> ERROR!


using namespace std;
int main(){ A void pointer cannot be
void *vp; dereferenced
int a = 8;
float b = 90.5;
vp=&a;
cout << *vp;
vp = &b;
cout << *vp;
return 0;
}
1.4. #include <iostream> ERROR!
using namespace std;
int main(){ Cannot assign pointer variable
int value = 95; to integer variable.
int *a, b;
a = &value;
b = a;
cout<<*b;
return 0;
}

Question 2 [3+3+4+5=15 Marks]

What will be the output of the given programs? In case of an error(s), circle that part in the code and
mention the reason for that error(s) in one line.

S no. Question Output

2.1. #include <iostream> 81


using namespace std;
int MyFunction(int s, int t)
{
if (t != 0){
return (s*MyFunction(s, t-1));
}
else{
return 1;
}
}
int main(){

cout<<”Output”<<MyFunction(3,4);

return 0;
}
2.2 #include <iostream>
using namespace std;
6

int MyFunction(int n1, int n2) {

if (n2 != 0)

return MyFunction(n2, n1 % n2);

else

return n1;

int main(){

cout<<”Output”<<MyFunction(366,60);

return 0;
}
2.3 #include <iostream>
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";

print_asterisk(asterisk - 1);
}
void print_space(int space)
{
if (space == 0)
return;
cout << " "
<< " ";

print_space(space - 1);
}

void pattern(int n, int num)


{
if (n == 0)
return;

print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
cout << endl;
pattern(n - 1, num);
}

int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Question 2.4 [5 Marks]

Write a recursive function using c++ to print how many times digit 5 appear in octal representation of decimal
number.

void decToOct(int n) int main()


{ {

int n=5;
if( n == 0)
return 0; cout<<decToOct(n); //print
if((n%8)==5)
1
return 1+decToOct( n / 8);
return 0+decToOct( n / 8); n=893;
cout<<decToOct(n);//print 2
return 0;
}

Change the void return type to int. }


Question 3 [5+5+5=15 Marks]

What will be the output of the given programs? In case of an error(s), circle that part in the code and
mention the reason for that error(s) in one line.

S no. Question Output

3.1. #include <iostream> Striker>10 Next@50 Last@40


using namespace std;
Reset To0
int main()
{
int track[] = { 10, 20, 30, 40 },
*striker;
striker = track;
track[1] += 30;
cout << "Striker>" << *striker << " ";
*striker -= 10;
striker++;
cout << "Next@" << *striker << " ";
striker += 2;
cout << "Last@" << *striker << " ";
cout << "Reset To" << track[0] << " ";
return 0;
}
#include <iostream> BBB
using namespace std;
#include <stdio.h>
int main()
{
char* str[] = { "AAAAA", "BBBBB",
"CCCCC", "DDDDD" };
char** sptr[] = { str + 3, str + 2, str
+ 1, str };
3.2 char*** pp;
pp = sptr;
++pp;
cout<<**++pp + 2;
return 0;
}

3.3 #include <iostream> Compilation Error: the line 3 and 5


using namespace std; produce error because a pointer to
int main(){ constant can't change value at
const int i = 20; address and address of a constant
const int* const ptr = &i; pointer can't be changed
(*ptr)++; respectively
int j = 15;
ptr = &j;
cout << i;
return 0;
}
Question 4 [15 Marks]

What will be the output of the given programs? In case of an error(s), circle that part in the code and
mention the reason for that error(s) in one line.

#include <iostream>
using namespace std;

struct someStruct
{
int x = 1;
int y = 10;
char** z;
someStruct* ptr;

}s1 = { 3,10,0,0 };

int main() {
s1.z= new char* [s1.x];
s1.z[0] = (char*)"345";
s1.z[1] = (char*)"*&()&";
s1.z[2] = (char*)"**ur50";

s1.ptr = new someStruct{ 1,8 };


s1.ptr->z = new char* [s1.ptr->x];
s1.ptr->z[0] = (char*)"c001";

s1.ptr->ptr = &s1;
someStruct* s = &s1;

do {
cout << s->z[s->x - 1];
s = s->ptr;
} while (s != &s1);

return 0;
}
Output:

**ur50c001

You might also like