Sunteți pe pagina 1din 7

Week-2: Assignment-2

Due date: 4th 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
Name

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 */

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]

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


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

4. Which of the following is called address operator?


a) *
b) &
c) _
d) %

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 an b
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

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

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

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

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

11. The operator used for dereferencing or indirection is ____


a) *
b) &
c) ->
d) –>>

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

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;

14. Values that are used to end loops are referred to as ------------- values.
a) closing
b) ending
c) sentinel
d) stop

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
16. A pointer can be initialized with

a) Null
b) Zero
c) Address of an object of same type
d) All of them

17. Referencing a value through a pointer is called

a) Direct calling
b) Indirection
c) Pointer referencing
d) All of the above
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
19. How many minimum numbers of functions need to be presented in c++?

a) 0
b) 1
c) 2
d) 3
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;
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
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
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
24. What will be the utput 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
b) sukant
sukant
c) Compile time error
d) runtime error

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

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]


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

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]

S-ar putea să vă placă și