[#3] – C++ Fundamentals – Questions

C-plus-plus-programming-questions-techhyme

C++ is a powerful programming language. Every programming language follows certain rules which are required for writing even a simple error free programs. In order to write any C++ program, we must be aware of the available keywords, datatypes and have knowledge of variables, constants etc. Since C++ is a superset of C so most of the rules and regulations of C language is also valid in C++.

Index:

In addition to supporting some of the constructs of C, C++ supports certain additional constructs and improvements like strict type checking, allowing declaration of variable anywhere within the program etc.

Key Points To Remember –

  1. Tokens are the C++ program elements which are identified by the compiler. Token supported in C++ includes keywords, constants, special characters, operators etc.
  2. A constant is a value that doesn’t change during the program execution. The constants can be classified as integer constants, floating point constants, character constants and string constants.
  3. An identifier consists of sequence of letters, digits and underscore. They are case sensitive which represents the name of the variables used in a program. An identifier cannot begin with a digit.
  4. Reserved words that have standard predefined meaning in C++ are called keywords.
  5. A variable is a symbolic name given to a location in the computer’s memory where a value can be stored which can be used in the program.
  6. Initializing a variable when it is declared is regarded as a good programming practice.
  7. The basic difference between the variable declaration and variable definition is that in the former no memory is allocated but in later memory is allocated to the variable. A variable definition consists of a data type followed by one or more variable names ending with a semicolon
  8. An expression is a combination of constants, variables and operators to form an executable statement which computers a value.
  9. The symbols used to perform certain operations are called operators and the combination of operators, constants and variables are called expressions.
  10. In C++ we can declare a variable anywher4e within the program. However, It is recommended to declare as close as possible to its first use.
  11. The various operators available in C++ are Arithmetic, Operators, Relational operators, conditional operators, unary operators and some additional C++ operators.
  12. A logical operator evaluates the logical expression to true or false depending upon expression evaluates to true or false.
  13. The compound assignment operators +=, -=, *=, /= and %= perform an arithmetic operations on a variable and store the results back into the variable.
  14. The increment (++) and decrement (–) operators are used for incrementing and decrementing integral and floating-point variables.
  15. Type conversion is the property of converting a variable of one data type to another data type. It is usually classified into two categories.
    • Implicit type conversion
    • Explicit type conversion or Type Casting
  16. In implicit type conversion, the operand of lower data type is converted to operand of higher data type automatically before the expression is evaluated.
  17. Type casting is a form of type conversion in which type of variable is forced to be changed by using cast operator.
  18. When a floating-point value is stored is an integer variable, then the floating-point value is converted to an integer value by truncating its decimal value.
  19. The hierarchy of operators refers to the order in which operates are evaluated within an expression.
  20. Associativity refers to the order in which the consecutive operations within the same precedence group are carried out.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. What is the difference between ‘B’ and “B” ?
    • ‘B’ is a character constant which contains only ‘B’. However, “B” is a string constant which contains two characters ‘B’ and ‘/0’.
  2. Suppose i is an integer variable which is assigned 32767. If we increment value of i by 1 then what will be the output ?
    • As we know that range of integrs is till.32767 so on adding I will make it out of range and it will start backwards so output will be -32768.
  3. Why will the output of the statement c = 5/9* ( f-32), always be 0 where c and f are temperatures in degree Celsius and Fahrenheit ?
    • As 5/9 is always equal to 0 because both are integer values. So type casting is necessary.
  4. What is the difference between Ivalue and rvalue ?
    • The lvalue of a variable is the address in memory. We may think lvalue as location value. The rvalue is the value of the variable. A variable can act as both lvalue and rvalue whereas a literal constant can only act as rvalue.Consider the following statements

      int a, b;

      a = 6;
      b = a;In the first assignment variable a serve as lvalue and in the second statement variable a act as rvalue.
  5. What is the difference between precedence and associativity of operators ?
    • The hierarchy of operators refers to the order in which operators are evaluated within an expression whereas associativity refers to the order in which the consecutive operations within the same precedence group are carried out.
  6. How can you access global variable in the local block of the program if both local and global variable have same name ?
    • Using Scope Resolution operator (:)
  7. What will be the output of statement cout<< ( 10 > 20 ) ? 10:20; ?
    • 0, the reason is that insertion operator (<<) has higher precedence than conditional operator (?:)
  8. Classify the types of operators based on the number of operands ?
    • Unary : The operator that uses a single operand.
    • Binary : The operator that uses two operands.
    • Ternary : The operator that uses three operands.

Other Similar Questions – 

  1. What are tokens ? Why are they useful ?
  2. What is an identifier ? How is user defined identifier different from standard identifier ?
  3. What are the rules for declaring valid Identifiers ?
  4. What are different types of constants used in C++ ?
  5. How are variables different from constants ?
  6. What are the character constants ? How does they differ from Integer Constants ?
  7. What is the difference between ‘D and “D” ?
  8. What is a variable ? What is the difference between variable, constants and keywords ?
  9. What are the basic data types available in C++ ?
  10. How do you calculate the integer data type ?
  11. Write short notes on the following data types:
    • Char
    • bool
    • float
    • double
  12. What is the difference between variable declaration and variable definition ?
  13. List the various escape sequences ?
  14. What is an operator ? List out of the various operators available in C++ ?
  15. What is an arithmetic operator ? Discuss the use of various arithmetic operators.
  16. What is the difference between unary and binary operators ?
  17. What is the basic differences between relational and logical operators ?
  18. What is a bitwise operator ? List various bitwise operator .
  19. How does assignment operator differs from equality operator ?
  20. Why conditional operator is known as ternary operator ?
  21. What is the difference between prefix and postfix increment operators ?
  22. Discuss the various unary operators ?
  23. Why do we need scope resolution operator ?
  24. What are different types of type conversion ?
  25. Using example explain the concept of Implicit type conversion ?
  26. What is type casting ?
  27. What is the importance of using precedence rules in C++ operators ?
  28. Explain the term Associativity ? When is it necessary ?
  29. List of operators having right to left associativity.
  30. List of operators having left to right associativity.
  31. Using examples explain the hierarchy of operators ?
  32. Write ‘True or ‘False’ against the following statements :
    • C++ has characters from ASCII character set.
    • A token is the smallest program element meaningful to the compiler.
    • Keywords are identifiers.
    • It is essential to append ‘f‘ or ‘F‘ at the end of the constant of type ‘float’.
    • Escape sequences are string constants.
    • The range of integer types lie in the range -2x-1 and 2x-1 where x is the number of bits for that type.
    • Float is the default floating-point type.
    • Variable initialization is same as variable assignment.
    • Explicit conversion is also known as widening conversion.
    • Unary operators have higher precedence than binary operators.
  33. Determine weather the following variable names are valid or invalid. If invalid, give the reason why.
    • flag
    • num
    • 12ab
    • while
    • pay rate
    • average
    • max_amount
    • valid
    • total
    • numIII
    • xyz
    • $tax
    • FOR
    • tta
    • max-item
  34. Write the declarations for the following statements:
    • Variable max_item that can store an integer value.
    • Variable percentage that can store a floating-point value.
    • Variable flag that can store boolean variables ( true or false).
    • Variables first, second and third that can store integer values.
    • Variables amount, tax and price that can store floating-point numbers.
    • An int constant SIZE with value 25.
    • A integer variable sum initialized to O.
    • A floating-point variable product initialized to 1.
  35. Evaluate the following expressions :
    Suppose x is an integer variable and s is a floating point variable. Assume x= 2 and Y=2.0

    • x = 44/6
    • x= 44%6 + 5*6-4
    • x= 50 + 46/7 * (12*5%2)
    • x= %= 4/x + 5
    • y + 5 + y * y * 10
    • y += 4 * 1.6 + (++x)
    • y = x++ + 3 * 1.6
  36. Write a C++ program that defines five integer variables and initializes them to 5, 10, 15, 20, 25 and display them on a single line separated by commas.
  37. Write a program that extract and prints the rightmost digit of the integral portion of a float.
  38. If x= 5, y=o and z= -5, what is the value of each of the following expressions.
    • x || y && z
    • (x || y) && z
    • (x && y) || z
    • x && y || z

Leave a Reply