OOP Sessional 1 Solution
OOP Sessional 1 Solution
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.
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;
}
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.
cout<<”Output”<<MyFunction(3,4);
return 0;
}
2.2 #include <iostream>
using namespace std;
6
if (n2 != 0)
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);
}
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.
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;
}
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.
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->ptr = &s1;
someStruct* s = &s1;
do {
cout << s->z[s->x - 1];
s = s->ptr;
} while (s != &s1);
return 0;
}
Output:
**ur50c001