Jesis Maharjan F A2
Jesis Maharjan F A2
Jesis Maharjan F A2
Roll no 6331
Name Jesis Maharjan
Ans:- c
2. Which of the following is the correct syntax of including a user defined header files in C+
+?
a) #include <userdefined.h>
b) #include <userdefined>
c) #include “userdefined”
d) #include [userdefined]
Ans:-c
Ans:-c
Ans:-c
a) yes
b) no
c) do nothing
d) yes and no
Ans:-c
Ans:-b
8. What is the output of the following code segment?
int x = 0;
for(x = 5; x < 8; ++x);
cout << x << " ";
cout << endl;
a) 5 6 7
b) 6 7
c) 5
d) 8
Ans:-a
9. How many times does the cout statement in the following code execute?
int a = 2;
int b = 6;
while(a < 10)
for(int b = 0; b < 4; ++b)
{
cout << a << " " << b << " ";
++a;
}
cout << endl;
a) 8
b) 12
c) 10
d) indefinitely
Ans:-d
10. What is the index number of the last element of an array with 9 elements?
a) 9
b) 8
c) 0
d) Programmer-defined
Ans:-b
11. The operator used for dereferencing or indirection is ____
a) *
b) &
c) ->
d) –>>
Ans:-a
Ans:-d
13. If you declare an integer pointer as int* pt; and you declare an integer variable as
int num; , then which of the following is legal?
a) num = &pt;
b) pt = #
c) *num = *pt;
d) &num = pt;
Ans:-c
14. Values that are used to end loops are referred to as ------------- values.
a) closing
b) ending
c) sentinel
d) stop
Ans:-a
Ans:-a
16. A pointer can be initialized with
a) Null
b) Zero
c) Address of an object of same type
d) All of them
Ans:-a
a) Direct calling
b) Indirection
c) Pointer referencing
d) All of the above
Ans:-b
18. What is the scope of the variable declared in the user defined function?
a) Whole program
b) Only inside the {} block
c) The main function
d) None of the above
Ans:-b
a) 0
b) 1
c) 2
d) 3
Ans:-b
20. Which of the following gives the memory address of the first element in array?
a) array[0];
b) array[1];
c) array(2);
d) array;
Ans:-a
21. What is the output of this program?
#include <stdio.h>
using namespace std;
int main()
{
char str[5] = "ABC";
cout << str[3];
cout << str;
return 0;
}
a) ABC
b) ABCD
c) AB
d) None of the mentioned
Ans:-a
22. Which of the following gives the [value] stored at the address pointed to by the pointer :
ptr?
a) Value(ptr)
b) ptr
c) &ptr
d) *ptr
Ans:-d
Ans:-a
#include <iostream>
#include <string.h>
int main()
{
struct student
int num;
char name[25];
};
student stu;
stu.num = 123;
strcpy(stu.name, "sukant");
return 0;
a) 123
sukant
a) sukant
sukant
c) Compile time error
d) runtime error
Ans:-a
Ans:-a
Write C++ programs for the following questions given. Your answer should also include
output of programs.
1. Write a program that reads a Celsius degree in a double value from the console, then
converts it to Fahrenheit and displays the result. The formula for the conversion is as
follows:
fahrenheit = (9 / 5) * celsius + 32
#include<iostream>
using namespace std;
int main()
{
float fahrenheit, celsius;
Sample Output:
=====================================
Italian Woods Furniture Company
=====================================
Enter P for pine, O for oak or M for mahogany : M
How many tables do you want : 3
#include<iostream>
using namespace std;
int main()
{
int n;
char answer;
cout<<"How many tables do you want?"<<endl;
cin>>n;
cout<<"=============================" <<endl;
cout<<"Italian Woods Furniture company"<<endl;
cout<<"==============================="<<endl;
cout<<"Enter 'P' for pine"<< endl;
cout<<"Enter 'O' for oak"<< endl;
cout<<"Enter 'M' for mahogany"<< endl;
cout<< "Enter 0 if you want to quit"<< endl;
cin>> answer;
switch(answer)
{
case 'P':
cout<<"Wood selected:Pine "<<"\tNo of tables: "<<n<<"\ttotal cost:
$"<<n*100<<endl;
cout<<"============================"<<endl;
cout<<"Thank you For visiting us."<<endl;
break;
case 'O':
cout<<"Wood selected: oak"<<"\tNo of tables: "<<n<<"\ttotal cost:
$"<<n*225<<endl;
cout<<"============================"<<endl;
cout<<"Thank you For visiting us."<<endl;
break ;
case 'M':
cout<<"Wood selected: mahogany"<<"\tNo of tables:"<<n<<"\ttotal
cost:$"<<n*310<<endl;
cout<<"============================"<<endl;
cout<<"Thank you For visiting us."<<endl;
break ;
case '0':
return 0;
break;
default:
cout<<"Something is wrong. Please try again\n"<<endl;
}
}
3. Create a structure named Apartment that contains data fields to hold the number of
bedrooms, the number of baths, and the monthly rent for the Apartment. Write a program
that creates an Apartment object and prompts the user for number of bedrooms and baths
desired. Determine the rent from the following table and set the rent field appropriately.
If a requested apartment type is not available, set the rent field to 0.
Display all the data including an error message if the entered data is invalid, or if no
apartments are available with the requested combination. [20 marks]
#include<iostream>
using namespace std;
struct apartment
{
int beds,baths,r;
};
int main()
{
struct apartment a;
cout<<"Enter the number of beds: ";
cin>>a.beds;
cout<<"Enter the number of baths: ";
cin>>a.baths;
if (a.beds>0 && a.beds<4 && a.baths>0 &&a.baths<3)
{
if (a.beds==1)
{
if(a.baths==1)
a.r=650;
else
a.r=0;
}
else if (a.beds==2)
{
if(a.baths==1)
a.r=829;
else
a.r=925;
}
else
{
if (a.baths==1)
a.r=0;
else
a.r=1075;
}
cout<<"\nNumber of beds: "<<a.beds;
cout<<"\nNumber of baths: "<<a.baths;
cout<<"\nMonthly rent for the apartment: $"<<a.r;
if(a.r==0)
cout<<"\nRequested apartment type is not available.";
}
else
{
cout<<"\nNumber of beds: "<<a.beds;
cout<<"\nNumber of baths: "<<a.baths;
cout<<"\nError!entered data is invalid.";
}
return 0;
}