Posts C/C++ Tricky Programs (Set 2)
Post
Cancel

C/C++ Tricky Programs (Set 2)


  • How to write a program to find a number even or odd without using division or modulus operators.

    Bitwise AND the number with 1. If the result is 0, then it is an odd number else it is an even number.
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h> 
  
int main() 
{ 
   unsigned int x = 3; 
   if (x&1) 
       printf("ODD"); 
   else
       printf("EVEN"); 
   return 0; 
} 

Output

1
ODD

  • What is the precedence when there are a Global variable and a Local variable in the program with the same name?

    Whenever there is a local variable with the same name as that of a global variable, the compiler gives precedence to the local variable.
1
2
3
4
5
6
7
8
#include <iostream.h>
int globalVar = 2;
int main()
{
 int globalVar = 5;
 cout<<globalVar<<endl;
}

Output

1
5

  • When there are a Global variable and Local variable with the same name, how will you access the global variable?

    When there are two variables with the same name but different scope, i.e. one is a local variable and the other is a global variable, the compiler will give preference to a local variable.
    In order to access the global variable, we make use of a “scope resolution operator (::)”. Using this operator, we can access the value of the global variable.
1
2
3
4
5
6
7
8
9
#include<iostream.h>
int x= 10;
int main()
{
 int x= 2;
 cout<<”Global Variable x = “<<::x;
 cout<<”\nlocal Variable x= “<<x;
}

Output

1
2
Global Variable x = 10
local Variable x= 2

  • How many ways are there to initialize an int with a Constant?

1
2
3
4
5
6
7
There are two ways:

    The first format uses traditional C notation.
    int result = 10;
    The second format uses the constructor notation.
    int result (10);


  • What are Default Parameters? How are they evaluated in the C++ function?

    Default Parameter is a value that is assigned to each parameter while declaring a function.
    This value is used if that parameter is left blank while calling to the function. To specify a default value for a particular parameter, we simply assign a value to the parameter in the function declaration.
    If the value is not passed for this parameter during the function call, then the compiler uses the default value provided. If a value is specified, then this default value is stepped on and the passed value is used.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h> 
int multiply(int a, int b=2)
{
              int r;
              r = a * b;
              return r;
} 
  
int main()
 {
  
             Cout<<multiply(6);
             Cout<<”\n”;
             Cout<<multiply(2,3);
 }

Output

1
2
12
6

  • C++ Inline Functions

    Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword “inline”.
    Such functions are advantageous only when the code of the inline function is small and simple. Although a function is defined as Inline, it is completely compiler dependent to evaluate it as inline or not.

To Read More About it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
using namespace std;

inline int Max(int x, int y) {
   return (x > y)? x : y;
}

// Main function for the program
int main() {
   cout << "Max (20,10): " << Max(20,10) << endl;
   cout << "Max (0,200): " << Max(0,200) << endl;
   cout << "Max (100,1010): " << Max(100,1010) << endl;
   
   return 0;
}

Output

1
2
3
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010

  • Difference between delete and delete[].

    “delete[]” is used to release the memory allocated to an array which was allocated using new[]. “delete” is used to release one chunk of memory which was allocated using new.
1
2
3
4
5
6
7
8
// create and destroy an int
int *b = new int;
delete b;

// create and destroy an int[1]
int *c = new int[1];
delete[] c;

NOTE:

What is wrong with this code?
1
2
T *p = new T[10];
delete p;
1
2
3
4
5
// The above code is syntactically correct and will compile fine.
// The only problem is that it will just delete the first element of 
// the array. Though the entire array is deleted, only the destructor 
// of the first element will be called and the memory for the first 
// element is released.

  • What is wrong with this code?

1
2
T *p = 0;
delete p;
1
2
In the above code, the pointer is a null pointer. 
Hence, the program will crash in an attempt to delete the null pointer.

  • Storage Classes in C++.

    Storage class determines the life or scope of symbols such as variable or functions.
1
2
3
4
5
6
7
8
9
10
C++ supports the following storage classes:
    Auto
    Static
    Extern
    Register
    Mutable

//The variable of a constant class object’s member cannot be changed. 
//However, by declaring the variables as “mutable”, 
//we can change the values of these variables.

To Read More About it


  • What is a Static Variable?

    A static variable is a local variable that retains its value across the function calls. Static variables are declared using the keyword “static”. Numeric variables which are static have the default value as zero.
1
2
3
4
5
6
7
8
//The following function will print 1 2 3 if called thrice.

void f()
{
static int i;
++i;
printf(“%d “,i);
}

Source

GeeksforGeeks
Quora
Software Testing Help
Tutorialspoint
This post is licensed under Ravi Jain by the author.