Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Assignment: Variables and Data Types

Assignment: Variables and Data Types | Basics of C++ - Software Development PDF Download

Instructions 

Answer the following questions by selecting the correct option or filling in the blanks with the appropriate answers. For hands-on questions, write the code snippets to fulfill the given requirements

Multiple Choice Questions (MCQs)

Q.1. Which of the following is NOT a valid C++ data type?
(a) float
(b) string
(c) double
(d) char

Assignment: Variables and Data Types | Basics of C++ - Software Development  View Answer

Ans. (b)

Q.2. What is the size of the "int" data type in C++?
(a) 4 bytes
(b) 2 bytes
(c) 8 bytes
(d) It varies depending on the system

Assignment: Variables and Data Types | Basics of C++ - Software Development  View Answer

Ans. (d)

Q.3. Which of the following is the correct way to declare a variable in C++?
(a) var x;
(b) x = 5;
(c) int x;
(d) 5 = x;

Ans. (c)

Q.4. What is the value of the variable "result" after executing the following code snippet?
int num1 = 10;
int num2 = 3;
double result = num1 / num2;
(a) 3.0
(b) 3
(c) 3.3333
(d) Compilation error

Ans. (b)

Q.5. Which operator is used to find the remainder of a division operation in C++?
(a) /
(b) %
(c) *
(d) $

Ans. (b)

Higher Order Thinking Questions (HOTS)

Q.1. Write the correct C++ code to swap the values of two variables, x and y, without using a temporary variable.

Assignment: Variables and Data Types | Basics of C++ - Software Development  View Answer

int x = 5;

int y = 10;

x = x + y;

y = x - y;

x = x - y;

Q.2. Explain the difference between declaring a variable and initializing a variable.

Assignment: Variables and Data Types | Basics of C++ - Software Development  View Answer

Declaring a variable means creating its name and specifying the data type, while initializing a variable means assigning a value to it.

Q.3. What are the basic differences between the "float" and "double" data types in C++?

The "float" data type in C++ is a single-precision floating-point type that occupies 4 bytes of memory, while the "double" data type is a double-precision floating-point type that occupies 8 bytes of memory. The "double" data type can represent larger and more precise decimal numbers compared to "float."

Q.4. Write a C++ code snippet to calculate the area of a circle, given the radius as input from the user.

#include <iostream>

using namespace std;


int main() {

    double radius;

    double area;

    const double pi = 3.14159;


    cout << "Enter the radius of the circle: ";

    cin >> radius;


    area = pi * radius * radius;


    cout << "The area of the circle is: " << area << endl;


    return 0;

}

Q.5. How can you convert an integer variable to a string in C++? Provide an example.

#include <iostream>

#include <string>

using namespace std;


int main() {

    int num = 12345;

    string str = to_string(num);


    cout << "The converted string is: " << str << endl;


    return 0;

}

Fill in the Blanks

Q.1. The process of reserving memory space for a variable is called __________.

Ans. Allocation

Q.2. The _________ data type in C++ is used to store single characters.

Ans. char

Q.3. The _________ operator is used to access the memory address of a variable.

Ans. & (ampersand)

Q.4. The __________ function in C++ is used to read input from the user.

Ans. cin

Q.5. A variable that can hold the address of another variable is called a ________ variable.

Ans. pointer

True or False

Q.1. In C++, the variable names are case-insensitive.

Ans. False

Q.2. The "cin" statement is used to output data in C++.

Ans. False

Q.3. A character variable can store multiple characters.

Ans. False

Q.4. The "sizeof" operator returns the size of a variable in bytes.

Ans. True

Q.5. All variables in C++ must be declared before they can be used.

Ans. True

Hands-On Questions

Q.1. Write a C++ program that declares two integer variables, x and y, and assign values of your choice to them. Print their sum.

#include <iostream>

using namespace std;


int main() {

    int x = 5;

    int y = 10;

    int sum = x + y;


    cout << "The sum of x and y is: " << sum << endl;


    return 0;

}

Q.2. Write a C++ program that calculates and prints the average of three floating-point numbers input by the user.

#include <iostream>

using namespace std;


int main() {

    float num1, num2, num3;

    float average;


    cout << "Enter three numbers: ";

    cin >> num1 >> num2 >> num3;


    average = (num1 + num2 + num3) / 3;


    cout << "The average is: " << average << endl;


    return 0;

}

Q.3. Write a C++ program that prompts the user to enter their name and age. Then, print a message that says "Hello, <name>! You are <age> years old."

#include <iostream>

#include <string>

using namespace std;


int main() {

    string name;

    int age;


    cout << "Enter your name: ";

    cin >> name;


    cout << "Enter your age: ";

    cin >> age;


    cout << "Hello, " << name << "! You are " << age << " years old." << endl;


    return 0;

}

Q.4. Write a C++ program that takes a temperature value in Celsius and converts it to Fahrenheit. The formula to convert Celsius to Fahrenheit is: F = (C * 9/5) + 32.

#include <iostream>

using namespace std;


int main() {

    double celsius, fahrenheit;


    cout << "Enter temperature in Celsius: ";

    cin >> celsius;


    fahrenheit = (celsius * 9 / 5) + 32;


    cout << "Temperature in Fahrenheit: " << fahrenheit << endl;


    return 0;

}

Q.5. Write a C++ program that calculates and prints the factorial of a number entered by the user. The factorial of a non-negative integer n is denoted by n!, and it is the product of all positive integers less than or equal to n.

#include <iostream>

using namespace std;


int main() {

    int number;

    int factorial = 1;


    cout << "Enter a number: ";

    cin >> number;


    for (int i = 1; i <= number; i++) {

        factorial *= i;

    }


    cout << "The factorial of " << number << " is: " << factorial << endl;


    return 0;

}

The document Assignment: Variables and Data Types | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

FAQs on Assignment: Variables and Data Types - Basics of C++ - Software Development

1. What are variables in software development?
Ans. Variables in software development are placeholders used to store data values. They have a specific data type, such as integer, string, or boolean, and can be manipulated and accessed throughout the program.
2. How are data types used in programming?
Ans. Data types in programming define the type of data that a variable can hold, such as numbers, characters, or boolean values. They help ensure that operations are performed correctly and efficiently on the data.
3. What is the difference between local and global variables?
Ans. Local variables are defined within a specific function or block of code and can only be accessed within that scope. Global variables, on the other hand, are defined outside of any function and can be accessed from anywhere in the program.
4. How do you declare a variable in programming languages like C++ or Java?
Ans. In languages like C++ or Java, variables are declared by specifying the data type followed by the variable name. For example, in Java, you would declare an integer variable as "int myVariable;"
5. Why is it important to choose the correct data type for a variable?
Ans. Choosing the correct data type for a variable is important because it affects the amount of memory allocated for the variable, the range of values it can hold, and the operations that can be performed on it. Using the appropriate data type can help optimize the performance and functionality of the program.
70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

Exam

,

Assignment: Variables and Data Types | Basics of C++ - Software Development

,

Objective type Questions

,

Assignment: Variables and Data Types | Basics of C++ - Software Development

,

Assignment: Variables and Data Types | Basics of C++ - Software Development

,

Free

,

MCQs

,

Semester Notes

,

video lectures

,

ppt

,

past year papers

,

Summary

,

mock tests for examination

,

study material

,

Sample Paper

,

practice quizzes

,

Important questions

,

shortcuts and tricks

,

Viva Questions

,

Extra Questions

,

pdf

;