Posts

Showing posts with the label Object Oriented Programming

What is the value assigned to the variable in each of these cases? Explain curious results. Be careful!

What is the value assigned to the variable in each of these cases? Explain curious results. Be careful! int x, y; a) x = 1/2; b) y = 3.0/2.0; double z, w, t; c) z = 1/2; d) w = 3/2; e) t = 3.0/2.0; Answer: a) 0 is assigned.1/2 is computed using truncating integer divide which gives 0. b) 1.5 is calculated and assigned. The fractional part is discarded; 1 is stored. c) 0 is calculated because 1/2 is done with truncating integer divide, gives 0. The floating point value 0.0 is stored. d) 1 is calculated, because truncating int divide, 1.0 is stored e) 1.5 is calculated and stored.

When you use a double, what is doubled? When you use a long int, how much longer is the long int than an int? Comment.

When you use a double, what is doubled? When you use a long int, how much longer is the long int than an int? Comment. Answer: In a double, the precision part of the value is about twice the precision of a float. The size of the exponent is also increased. In most implementations, long is the same size as int.

Write a short program that contains statements to output the values of a variable that you define but neither initialize nor assign. Discuss the output you get.

Write a short program that contains statements to output the values of a variable that you define but neither initialize nor assign. Discuss the output you get.  Answer: #include <iostream> using namespace std; int main() {   int uninitialized;   cout << uninitialized << endl;   return 0; }

Give the declaration for two variables, feet and inches. Declare each to be of type int, and both initialized to zero in the declaration. Give both initialization alternatives.

Give the declaration for two variables, feet and inches. Declare each to be of type int, and both initialized to zero in the declaration. Give both initialization alternatives. Answer: int feet = 0; int inches = 0; //Alternate initialization. int feet (0); int inches(0);

What is the difference between a warning from the compiler and an error message from the compiler?

 What is the difference between a warning from the compiler and an error message from the compiler? Answer: An error message usually causes code generation to stop. A warning does not. Unless you understand what the warning is all about you should not ignore warnings.

What does the line #include do for your program?

What does the line #include <iostream> do for your program? Answer: The #include directive provides declarations and definitions of the iostream components so that your program can use them in a way that allows the system linker to connect your program to the corresponding library.

Given the C++ output statements. What is the output of these lines of code? Explain.

Given the C++ output statements. What is the output of these lines of code? Explain.  cout << "If you have "; cout << "a number of pods "; cout << "you can quit."; cout << "\n"; Answer: If you have a number of pods you can quit. Explanation: Note that there is a <cr> emitted by the code, which puts the next output position on the next line at the left end.

Write a C++ program that outputs "My first C++ program" and then outputs a carriage return.

Write a C++ program that outputs "My first C++ program" and then outputs a carriage return. Answer: #include <iostream> using namespace std; int main() {   cout << “My first C++ program” << endl; } Explanation: An alternative is to place using namespace std; after the #include directive, or to place this inside the block of the function main. This depends on the local rules for placing namespace directives and definitions.

In the history of the C++ language, what was the immediate predecessor of the C++ language? How is this older language related to C++?

In the history of the C++ language, what was the immediate predecessor of the C++ language? How is this older language related to C++? Answer: The immediate predecessor of C++ is the C programming language. C is very nearly a proper subset of C++.