Jesis Maharjan F A2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Week-2: Assignment-2

Due date: 20th June 2020 – [11:59PM]


NOTE : Submit your solution in microsoft word files only
All programming question solutions must be followed by output screen shots
Don’t forget to type your roll no and names.

Roll no 6331
Name Jesis Maharjan

Complete all the tasks as per the instructions given.

Task-1(MCQ) – [50 marks]


===========
Choose the correct option. Every question carries 2 marks

1. Which of the following is used for comments in C++?


a) // comment
b) /* comment */
c) both // comment or /* comment */
d) // comment */

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

3. Which of the following is a correct identifier in C++?


a) 7var_name
b) 7VARNAME
c) VAR1234
d) $var_name

Ans:-c

4. Which of the following is called address operator?


a) *
b) &
c) _
d) %
Ans:-b

5. Which of the following declares a char named constant called TOP_GRADE?


a) const char TOP_GRADE = 'A';
b) const char TOP GRADE = 'A';
c) const char TOP_GRADE;
d) both a and b

Ans:-c

6. What is the output after executing the following segment of code?

int num = 10;


if (num > 10)
cout << "Yes" << endl;
cout << "No" << endl;

a) yes
b) no
c) do nothing
d) yes and no

Ans:-c

7. What is the output after executing the following segment of code?

int age = 30;


if(age = 40)
cout << "Boy that is old!";
else
cout << "Some day you will be old too";

a) Boy that is old


b) Some day you will be old too
c) Compile time error
d) anything but not a and b options

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

12. You use a subscript to ___________ .


a) indicate a position within an array
b) identify empty classes
c) define classes
d) locate a variable at a specific, desired memory address

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 = &num;
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

15. Choose the right option


string* x, y;

a) x is a pointer to a string, y is a string


b) y is a pointer to a string, x is a string
c) Both x and y are pointers to string types
d) none of the above

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

17. Referencing a value through a pointer is called

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

19. How many minimum numbers of functions need to be presented in c++?

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

23. What will happen when the structure is declared?


a) it will not allocate any memory
b) it will allocate the memory
c) it will be declared and initialized
d) it will be declared

Ans:-a

24. What will be the output of the following c++ code?

#include <iostream>

#include <string.h>

using namespace std;

int main()
{

struct student

int num;

char name[25];

};

student stu;

stu.num = 123;

strcpy(stu.name, "sukant");

cout << stu.num << endl;

cout << stu.name << endl;

return 0;

a) 123
sukant
a) sukant
sukant
c) Compile time error
d) runtime error

Ans:-a

25. What will be the output of the following code?


#include<iostream>
using namespace std;
int a =5;
int func(int a)
{
return a++;
}
int main()
{
int b;
b = func(a);
cout << a <<” ”<<b<< endl;
}
a) 5 5
b) 6 5
c) 5 6
d) 6 6

Ans:-a

Task-2 [Programming Exercise] [50 marks]


=====

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

Hint: In C++, 9 / 5 is 1, but 9.0 / 5 is 1.8. [10 marks]

#include<iostream>
using namespace std;

int main()
{
float fahrenheit, celsius;

cout << "Enter the temperature in Celsius : ";


cin >> celsius;
fahrenheit = (celsius * 9.0) / 5.0 + 32;
cout << "The temperature in Celsius : " << celsius << endl;
cout << "The temperature in Fahrenheit : " << fahrenheit << endl;
return 0;
}
2. Write a program for a furniture company. Ask the user to choose P for pine, O for oak, or
M for mahogany. Then ask the user how many pieces do you want? Then show the total
billing price of a table/s manufactured with the chosen wood as the format shown below.
Pine tables cost $100, oak tables cost $225, and mahogany tables cost $310. [ Hint :
Declare 3 numeric constant values to store the wood costs] [20 marks]

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

Wood SelectedNo of tables Total cost


------------------ --------------- ------------
Mahogany 3 $930

Thank you for visiting us.


=====================================
Hint: use switch case

#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;
}

You might also like