V.Vidhya Logo

V.Vidhya

Operators & Expressions


Q1.

What will be the output of the following programs?

You have to predict the output yourself without running/executing the code.


Q2.

Write a program to calculate the perimeter and the area of a rectangle. Take the length and the width of a rectangle from user and then prints the value of perimeter and area of that given rectangle.

You can safely assume that the value of length and width will always be an integer.

Expected Output:

Enter the length & width of a rectangle: 10 5   // user input

Perimeter: 30
Area: 50

// Perimeter = 2 * (l + w) = 2 * (10 + 5) = 30
// Area = l * w = 10 * 5 = 50

Q3.

There are 10 seats placed in a circle for an event, numbered from 1 to 10. People are arriving and standing in a waiting line (queue). Each person is given a seat based on their position in the line.

Write a program where the user enters their position in the waiting line (queue) and the program tells them which seat number they will get.

(For e.g. if someone is the 11th in line, they should get seat 1 again because the seats are arranged in a circle. If someone is 12th, they get seat 2 and so on.)

Expected Output:

Enter your queue number: 23   // user input

Your seat number is: 3

Q4.

Write a program that takes the cost of any 3 food items from the user (e.g. a packet of biscuit, a packet of waffer, an icecream cone) and then -

Choose variable data types carefully to prevent any loss of precision in the output data.

Expected Output:

Enter the cost of 3 food items: 50 20 60   // user input
Total Cost: 130
GST Amount : 23.4
Total Bill: 153.4

// Total Cost = 50 + 20 + 60 = 130
// GST Amount = Total Cost * 18 / 100 = 130 * 18 / 100 = 23.4
// Total Bill = Total Cost + GST Amount = 130 + 23.4 = 153.4

Q5.

What will be the output of the following program?

You have to predict the output result without running/executing the code.

#include <iostream>

int main()
{
    const float gravity;
    gravity = 9.8;

    std::cout << "Gravity: " << gravity;

    return 0;
}
  1. 9.8
  2. Compile-time error
  3. Run-time error
  4. 9

Q6.

What will be the output of the following program?

You have to predict the output result without running/executing the code.

#include <iostream>

int main()
{
    int age = 5;
    const int constAge = age;

    age = 7;

    std::cout << "Age: " << age;
    std::cout << "Const Age: " << constAge;

    return 0;
}
  1. 55
  2. Compile-time error
  3. 75
  4. 76

Q7.

Write a program that takes a value of radius from user, then find the circumference and area of a circle for a given radius.

Take 3.14 as the value of pi.

Expected Output:

Enter the value of radius: 5   // user input

Circumference: 31.4
Area: 78.5

// Circumference = 2 * pi * r = 2 * 3.14 * 5 = 31.4
// Area = pi * r * r = 3.14 * 5 * 5 = 78.5

Q8.

Write a program that takes a value of temperature in Celsius from user, then convert it into Kelvin and Fahrenheit.

The formulas for Celsius to Kelvin and Celsius to Fahrenheit are -

Expected Output:

Enter the value of temperature in Celsius: 30   // user input
temperature in Kelvin: 303
temperature in Fahrenheit: 86

// Kelvin = Celsius + 273 = 30 + 273 = 303
// Fahrenheit = 9/5 * Celsius + 32 = 9/5 * 30 + 32 = 86

Q9.

You’re helping a government planning department analyze how a country’s population is expected to grow over time. Based on the current population, an estimated annual growth rate and a time period in years, you need to calculate what the population will be at the end of that period.

Population growth can be calculated by looking at the change in population over time. The formula for population growth is -

pt = p0 x (1 + r)t

Where,

Expected Output:

Enter the initial population (in crores): 142   // user input
Enter the rate of growth (in %): 0.875   // user input
Enter the time (in years): 5   // user input

Total population after given time (in crores): 148.322

Q10.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Prev Post
Data Types
Next Post
Conditional Statements