V.Vidhya Logo

V.Vidhya

Data Types


Q1.

Pick the appropriate data type for a variable in each of the following situations. Be as specific as possible.

  1. The age of the user (in years)
  2. Whether the user wants the application to check for updates
  3. pi (3.14159265)
  4. The number of pages in a textbook
  5. The length of a couch in feet, to 2 decimal places
  6. A user selecting an option from a menu by letter

Q2.

Write a program where the user is asked to enter marks for 5 subjects. Calculate and print the total marks and percentage.

Expected Output:

Enter marks for 5 subjects: 85 90 78 92 88   // user input

Total marks: 433
Percentage: 86.60%

Q3.

What will be the output of the following programs?

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

#include <iostream>

int main() 
{
    int a = b;
    int b = 2;
    
    std::cout << "a: " << a << '\n';
    std::cout << "b: " << b << '\n';
    
    return 0;
}

Q4.

Write a program where the user is asked to enter distance (in kilometers) and time (in hours). Print the calculated speed.

Expected Output:

Enter distance (in km): 110   // user input
Enter time (in hours): 4   // user input

Speed: 27.5 km/h

Q5.

What will be the output of the following programs?

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


Q6.

Write a program where the user is asked to enter their weight (in kg) and height (in meters). Print the value of BMI.

The formula for BMI is: BMI = weight / (height x height)

Expected Output:

Enter your weight (kg): 70   // user input
Enter your height (m): 1.75   // user input

Your BMI: 22.86

Q7.

You are part of a school where students are grouped into divisions like A, B, C, etc. Write a program that asks the user to enter the division they are in (as a single character) and then prints the entered division.

Expected Output:

Enter your division (A, B, C, etc): B   // user input
You are in Division B.

Q8.

Some forms ask you to mention whether you are a student or not. Write a program that asks the user if they are a student, stores the answer and displays the same.

Make sure your program works with both types of responses:

  • 1 for Yes and 0 for No
  • true for Yes and false for No

Expected Output:

// case 1
Are you a student? (1 for Yes, 0 for No): 1   // user input
Student status: 1

// case 2
Are you a student? (true for Yes, false for No): false   // user input
Student status: false

Q9.

What will be the output of the following programs?

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


Q10.

Write a program that asks a user whether they want to give a tip at the restaurant or not and then prints their response on the bill.

User should be able to give their response in the form of true or false keywords and the response should also be printed in that form.

Expected Output:

Would you like to give a tip: false   // user input

Is user willing to give a tip: false

Q11.

Predict the output of the following programs for the given input values.

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


Q12.

Write a program where the user is asked to enter a single character. Print the value of the character and its ASCII code.

Expected Output:

Enter a single character: A   // user input
You entered 'A', which has an ASCII code 65.

Q13.

Consider the following program:

#include <iostream>

int main()
{
    std::cout << "Enter a number: ";
    int x;
    std::cin >> x;
    std::cout << "You entered " << x << '\n';

    return 0;
}

The program expects you to enter an integer value, that user input value will be put into an integer variabled as x.

Predict the output that results when you enter the following types of input:

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

  1. A letter, such as h.
  2. A number with a fractional part (e.g. 3.2). Try numbers with fractional parts less than 0.5 and greater than 0.5 (e.g. 3.2 and 3.7).
  3. A small negative integer, such as -3.
  4. A word, such as Hello.
  5. A really big number (at least 300 crores i.e. 3000000000 (3 with nine zeros)).
  6. A small number followed by some letters, such as 123abc.
  7. A few letters followed by a small number, such as abc123.
  8. +5 (three spaces, followed by a plus symbol, and a 5).
  9. 5b6.

Prev Post
Variables & User I/O
Next Post
Operators & Expressions