Basic C++ Syntax

  Home  C++ Programming  Basic C++ Syntax


“C++ Syntax frequently Asked Questions in various C++ Syntax job Interviews by interviewer. The set of questions here ensures that you offer a perfect answer posed to you. So get preparation for your new job hunting”



27 Basic C++ Syntax Questions And Answers

1⟩ Explain what are the Sizes and ranges of the Basic C++ data types?

Following table is with respect to a 16-bit word machine:

Type Bytes Range

char 1 -128 to 127

unsigned char 1 0 to 255

signed char 1 -128 to 127

int 2 -32768 to 32767

unsigned int 2 0 to 65535

signed int 2 2 -32768 to 32767

short int 2 2 -32768 to 32767

unsigned short int 2 0 to 65535

signed short int 2 -32768 to 32767

long int 4 4 -2147483648 to 2147483647

signed long int 4 -2147483648 to 2147483647

unsigned long int 4 0 to 4294967295

float 4 4 3.4E-38 to 3.4E+38

double 8 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to 1.1E+4932

 204 views

2⟩ What are the basics of local (auto) objects?

C++ extends the variable declaration syntax from built-in types (e.g., int

i;) to objects of user-defined types. The syntax is the same: TypeName VariableName.

For example, if the header file “Car.hpp” defines a user-defined type called Car,

objects (variables) of class (type) Car can be created:

#include "Car.hpp" // Define class Car

void f()

{

Car a; // 1: Create an object

a.startEngine(); // 2: Call a member function

a.tuneRadioTo("AM", 770); // 3: Call another member function

} // 4. Destroy the object

int main()

{

f();

}

When control flows over the line labeled 1: Create an object, the runtime

system creates a local (auto) object of class Car. The object is called a and can

be accessed from the point where it is created to the } labeled 4: Destroy the

object.

When control flows over the line labeled 2: Call a member function, the

startEngine() member function (a.k.a. method) is called for object a. The

compiler knows that a is of class Car so there is no need to indicate that the

proper startEngine() member function is the one from the Car class. For

example, there could be other classes that also have a startEngine() member

function (Airplane, LawnMower, and so on), but the compiler will never get

confused and call a member function from the wrong class.

 207 views

4⟩ How to Declaring Variables in C++?

To declare a variable you use the syntax "type <name>". Here are some variable declaration examples:

1- int x;

2- char letter;

3- float the_float;

It is permissible to declare multiple variables of the same type on the same line; each one should be separated by a comma.

1- int a, b, c, d;

If you were watching closely, you might have seen that declaration of a variable is always followed by a semicolon (note that this is the same procedure used when you call a function).</name>

 219 views

5⟩ What are the scope rules you observed in the program?taskc++ investigate the operation of scope rules in a program.// variablenamescope#include <iostream>using namespace std;int i = 111;int main(){{int i = 222;{int i = 333;cout << "i = " << i << endl;{int i = 444;cout << "i = " << i << endl;{cout << "i = " << i << endl;}}}cout << "i = " << i << endl;}cout << "i = " << i << endl;return 0;}

i want to say Global scope(int i = 111;),Local scope (int i = 333;), and function scope (int i = 222;) but now, i'm confused. I really don't understand what is this question asking me. Can somebody help me break it down

 187 views

7⟩ What OO language is best?

Whichever one works best for the organization.

We believe in honesty, not advocacy, and the honest answer is that there

is no single answer. What is the organization’s policy regarding languages?

Must there be one and only one official language? What is the skill level of

the staff? Are they gung-ho developers with advanced degrees in computer

science/engineering or people who understand the business and have survival

skills in software? Do they already have OO skills? In which language(s)? What

sort of software development is being done: extending someone else’s framework

or building from scratch for resale? What sort of performance constraints does

the software have? Is it space constrained or speed constrained? If speed, is it

typically bound by I/O, network, or CPU? Regarding libraries and tools, are

there licensing considerations? Are there strategic partnership relationships that

affect the choice of languages? Many of these questions are nontechnical, but

they are the kind of questions that need to be answered the “which language”

issue can be addressed.

Regarding the choice between C++ and Java, java is a simpler language and

thus it is generally easier to use. However C++ is more established and allows

finer control over resources (for example, memory management), and this is required

for some applications. Also, C++ has language features such as destructors

 199 views

8⟩ What’s the “Software Peter Principle”?

The Software Peter Principle is in operation when unwise developers “improve” and “generalize” the software until they themselves can no longer understand it, then the project slowly dies.

The Software Peter Principle can ruin projects. The insidious thing about the Software Peter Principle is that it’s a silent killer — by the time the symptoms are visible, the problem has spread throughout every line of code in the project.

Foolish managers deal with symptoms rather than prevention, and they think

everything is okay unless there are visible bugs. Yet the problem isn’t bugs, at least initially. The problem is that the project is collapsing under its own weight.

The best way to avoid this problem is to build to the skill level of the maintainers, not of the developers. If the typical maintainer won’t understand the software then it’s simply too complex for the organization to maintain. This means avoiding tricky, sophisticated, subtle, clever techniques unless there is a compelling reason for them. Cleverness is evil; use it only when necessary.

Shown concern for the long-term health of the system being developed.

 206 views

9⟩ What is the most common mistake on C++ and OO projects?

Unnecessary complexity — the plague of OO technology.

Complexity, like risk, is a fact of life that can’t be avoided. Some software

systems have to be complex because the business processes they represent are

complex. But unfortunately many intermediate developers try to “make things

better” by adding generalization and flexibility that no one has asked for or will

ever need. The customer wants a cup of tea, and the developers build a system

that can boil the ocean [thanks to John Vlissides for this quip]. The result

is unnecessary complexity, which increases the risk of failure. The intentions

might be good but the result can be deadly.

Here are a few guidelines.

• Don’t solve problems that don’t need to be solved.

• Don’t worry about the future until you’re sure you can survive the present.

39

• Don’t build things for the fun of it.

• The organization’s health is more important than the developer’s desire

to play with the latest whiz-bang tool or technique.

• Don’t add risk without a compelling and measurable benefit to the project.

• Don’t invest in the future if your current project is in trouble.

Avoid the “death by one thousands cut” syndrome by avoiding unnecessary

complexity.

 187 views

10⟩ What is basic if statement syntax?

The structure of an if statement is as follows:

if ( TRUE )

Execute the next statement

Here is a simple example that shows the syntax:

if ( 5 < 10 )

cout<<"Five is now less than ten, that's a big surprise";

 190 views

11⟩ What relational operators if statements in C++?

Here are the relational operators, as they are known, along with examples:

> greater than 5 > 4 is TRUE

< less than 4 < 5 is TRUE

>= greater than or equal 4 >= 4 is TRUE

<= less than or equal 3 <= 4 is TRUE

== equal to 5 == 5 is TRUE

!= not equal to 5 != 4 is TRUE

 208 views

12⟩ How to demonstrate the use of a variable?

Here is a sample program demonstrating the use of a variable:

#include <iostream>

using namespace std;

int main()

{

int thisisanumber;

cout<<"Please enter a number: ";

cin>> thisisanumber;

cin.ignore();

cout<<"You entered: "<< thisisanumber <<"n";

cin.get();

}

 205 views

13⟩ What is while loops?

while ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.

Example:

#include

using namespace std; // So we can see cout and endl

int main()

{

int x = 0; // Don't forget to declare variables

while ( x < 10 ) { // While x is less than 10

cout<< x <

x++; // Update x so the condition can be met eventually

}

cin.get();

}

 241 views

16⟩ What is Else If Syntax?

if ( ) {

// Execute these statements if is TRUE

}

else if ( ) {

// Execute these statements if is TRUE and

// is FALSE

}

 199 views

17⟩ What is Else Syntax in C++?

It can look like this:

if ( TRUE ) {

// Execute these statements if TRUE

}

else {

// Execute these statements if FALSE

}

 238 views

19⟩ What is functions Syntax in C++?

Functions that a programmer writes will generally require a prototype. Just like a blueprint, the prototype tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed. When I say that the function returns a value, I mean that the function can be used in the same manner as a variable would be. For example, a variable can be set equal to a function that returns a value between zero and four.

For example:

#include // Include rand()

using namespace std; // Make rand() visible

int a = rand(); // rand is a standard function that all compilers have

 188 views

20⟩ How to defines the function in C++?

When the programmer actually defines the function, it will begin with the prototype, minus the semi-colon. Then there should always be a block with the code that the function is to execute, just as you would write it for the main function. Any of the arguments passed to the function can be used as if they were declared in the block. Finally, end it all with a cherry and a closing brace. Okay, maybe not a cherry.

Let's look at an example program:

#include

using namespace std;

int mult ( int x, int y );

int main()

{

int x;

int y;

cout<<"Please input two numbers to be multiplied: ";

cin>> x >> y;

cin.ignore();

cout<<"The product of your two numbers is "<< mult ( x, y ) <<"n";

cin.get();

}

int mult ( int x, int y )

{

return x * y;

}

 198 views