Saturday, February 14, 2026

C++ Decision Making Statements: Predict Output Questions (For Beginners)

C++ Decision-making Statements

Predict Output Questions For Beginners 

(if, if-else, nested if, else if ladder, switch, etc.)

Wednesday, February 4, 2026

Popular C++ Function Practicals

Followings are popular C++ Function Practicals that every beginners must know.


1. Function to find Sum of Two Numbers.
Concept: Function with arguments and return value

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    int x, y;
    cout << "Enter two numbers: ";
    cin >> x >> y;
    cout << "Sum = " << add(x, y);
    return 0;
}

Output:

Enter two numbers: 5 6
Sum = 11

2. Function to check Even or Odd numbers.
Concept: Function with argument, no return value

#include <iostream>
using namespace std;

void checkEvenOdd(int n) {
    if (n % 2 == 0)
        cout << "Even number";
    else
        cout << "Odd number";
}

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    checkEvenOdd(num);
    return 0;
}

Output:
Enter a number: 5
Odd number

3. Function to find Factorial of a Number.
Concept: Recursive function

#include <iostream>
using namespace std;

int factorial(int n) {
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Factorial = " << factorial(n);
    return 0;
}

Output:
Enter a number: 4
Factorial = 24

4. Function to find Maximum of Two Numbers
Concept: Function with arguments and return value

#include <iostream>
using namespace std;

int maxNumber(int a, int b) {
    if (a > b)
        return a;
    else
        return b;
}

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "Maximum = " << maxNumber(a, b);
    return 0;
}

Output:
Enter two numbers: 5 6
Maximum = 6

5. Function to Swap Two Numbers (Call by Value)
Concept: Function basics (introduction before pointers)

#include <iostream>
using namespace std;

void swapNumbers(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    cout << "Inside function: a = " << a << ", b = " << b;
}

int main() {
    int x = 5, y = 10;
    swapNumbers(x, y);
    cout << "\nOutside function: x = " << x << ", y = " << y;
    return 0;
}

Output:

Inside function: a = 10, b = 5
Outside function: x = 5, y = 10


* * * * *

Sunday, February 1, 2026

C++ Functions - Syntax, Categories and Examples

What is a Function? 
  • A function in C++ is a block of code that performs a specific task. 

Key Benefits:
  • It helps in breaking a large program into smaller, manageable parts, improves code reusability, and makes programs easy to understand and maintain.
General Syntax of a Function

return_type function_name(parameter_list)
{

  // function body
  // statements

  return value; // optional (if return_type is void)
}

Key points
  • return_type: Data type of the value returned by the function (e.g., int, float, void)

  • function_name: Name of the function (should be meaningful)

  • parameter_list: Input values passed to the function (optional)

  • function body: Contains the logic of the function

  • return statement: Sends the result back to the calling function

Four Categories of Functions - Examples:


Category 1: Function without Parameters and without Return Value

#include <iostream>
using namespace std;

void greet() {
  cout << "Welcome to C++ Programming";
}

int main()
{
  greet();
  return 0;
}

Key Points: The function greet() simply prints a message. It does not take inputs or return any value.

Category 2: Function with Parameters and without Return Value

#include <iostream>
using namespace std;

void add(int a, int b) {
  cout << "Sum = " << a + b;
}

int main() {
  add(10, 20);
  return 0;
}

Key points: The function add() receives two integers and displays their sum.

Category 3: Function with Parameters and Return Value

#include <iostream>
using namespace std;

int square(int n) {
  return n * n;
}

int main() {
  int result = square(5);
  cout << "Square = " << result;
  return 0;
}

Key points: The function square() calculates and returns the square of a number.

Category 4: Function without Parameters but with Return Value

#include <iostream>
using namespace std;

int getNumber() {
  return 100;
}

int main() {
  cout << "Number = " << getNumber();
  return 0;
}

Key points: The function getNumber() returns a fixed integer value.

Details about Recursive Functions provided on a separate page.


* * * * *

Thursday, January 29, 2026

C++ Read two numbers: Find Difference and Sign Check

Write a C++ program that reads two integers from the user, calculates their difference, and checks whether the result is positive, negative, or zero. 

#include <iostream>

using namespace std;

int main() {
    int num1, num2, difference;

    // Input two integers
    cout << "Enter first integer: ";
    cin >> num1;
    cout << "Enter second integer: ";
    cin >> num2;

    // Calculate difference
    difference = num1 - num2;

    // Display result
    cout << "Difference = " << difference << endl;

    // Check whether the result is positive, negative, or zero
    if (difference > 0) {
        cout << "The difference is positive." << endl;
    } else if (difference < 0) {
        cout << "The difference is negative." << endl;
    } else {
        cout << "The difference is zero." << endl;
    }

    return 0;
}

/* Sample Output

Enter first integer: 50
Enter second integer: 20
Difference = 30
The difference is positive.

*/

Monday, January 5, 2026

C++ Classes and Objects: Predict Output

C++ Classes and Objects
Predict Output Questions


1. Constructor Initialization

#include <iostream>
using namespace std;

class Test {
    int x;
public:
    Test(int a) {
        x = a;
    }
    void display() {
        cout << x;
    }
};

int main() {
    Test obj(10);
    obj.display();
    return 0;
}

Answer:
10

Explanation:
  • Constructor assigns x = 10. The display() function prints the value of x.
2. Default Constructor
#include <iostream>
using namespace std;

class Demo {
    int x;
public:
    Demo() {
        x = 5;
    }
    void show() {
        cout << x;
    }
};

int main() {
    Demo d1;
    d1.show();
    return 0;
}

Answer:
5

Explanation:
Default constructor automatically sets x = 5 when object d1 is created.

3. Multiple Objects
#include <iostream>
using namespace std;

class Sample {
    int x;
public:
    Sample(int a) {
        x = a;
    }
    void display() {
        cout << x << " ";
    }
};

int main() {
    Sample s1(2), s2(4);
    s1.display();
    s2.display();
    return 0;
}

Answer:
2 4

Explanation:
  • Two objects are created with different values. Each object stores its own copy of x.
4. Static Data Member
#include <iostream>
using namespace std;

class Count {
    static int x;
public:
    Count() {
        x++;
    }
    static void show() {
        cout << x;
    }
};

int Count::x = 0;

int main() {
    Count c1, c2, c3;
    Count::show();
    return 0;
}

Answer:
3

Explanation:
  • x is static; hence shared among all objects.
  • Each constructor call increments x. Three objects; hence x = 3.

5. Function Returning Object
#include <iostream>
using namespace std;

class Test {
    int x;
public:
    Test(int a) {
        x = a;
    }
    Test add(Test t) {
        return Test(x + t.x);
    }
    void display() {
        cout << x;
    }
};

int main() {
    Test t1(5), t2(7);
    Test t3 = t1.add(t2);
    t3.display();
    return 0;
}

Answer:
12

Explanation:
  • t1.x = 5, t2.x = 7
  • New object created with value 5 + 7 = 12.

6. Constructor Called Multiple Times
#include <iostream>
using namespace std;

class Demo {
public:
    Demo() {
        cout << "C ";
    }
};

int main() {
    Demo d1, d2;
    return 0;
}

Answer:
C C

Explanation:
  • Constructor runs once per object. Two objects → prints twice.

7. Private Data Access
#include <iostream>
using namespace std;

class Test {
    int x;
public:
    void set(int a) {
        x = a;
    }
    void show() {
        cout << x;
    }
};

int main() {
    Test t;
    t.set(15);
    t.show();
    return 0;
}

Answer:
15

Explanation:
  • Private variable x is accessed using public member function set().

8. Object as Function Argument
#include <iostream>
using namespace std;

class Sample {
    int x;
public:
    Sample(int a) { x = a; }
    void add(Sample s) {
        cout << x + s.x;
    }
};

int main() {
    Sample s1(3), s2(6);
    s1.add(s2);
    return 0;
}

Answer:
9

Explanation: s1.x = 3, s2.x = 6 → prints 3 + 6 = 9.

9. Copy Constructor (Default)
#include <iostream>
using namespace std;

class Test {
    int x;
public:
    Test(int a) { x = a; }
    void show() { cout << x; }
};

int main() {
    Test t1(8);
    Test t2 = t1;
    t2.show();
    return 0;
}

Answer:
8

Explanation: Default copy constructor copies the value of x from t1 to t2.

10. Destructor Execution Order
#include <iostream>
using namespace std;

class Demo {
public:
    ~Demo() {
        cout << "D ";
    }
};

int main() {
    Demo d1, d2;
    return 0;
}

Answer:
D D

Explanation:
  • Destructor is called automatically when objects go out of scope (reverse order of creation).
  • Two objects → destructor runs twice.

< Back to C++ Home Page >

* * * * *

Thursday, December 25, 2025

MCQs on C++ Basics (Beginner Level)

MCQs based on Basics of C++ Programming
(For Beginners)


1. Which of the following is a correct multi-line comment?

A) <!-- comment -->
B) ## comment
C) // comment
D) /* comment */

Answer: D

2. C++ is an extension of which language?

A) Java
B) Python
C) C
D) Pascal

Answer: C

3. Which of the following is the correct file extension of a C++ program?

A) .c
B) .cpp
C) .java
D) .py

Answer: B

4. Which function is the entry point of a C++ program?

A) start()
B) run()
C) main()
D) program()

Answer: C

5. Which header file is required for input and output in C++?

A) stdlib.h
B) conio.h
C) iostream
D) string

Answer: C

6. Which namespace is commonly used in C++ programs?

A) system
B) std
C) main
D) global

Answer: B

7. Which operator is used for output in C++?

A) >>
B) <<
C) ::
D) ==

Answer: B

8. Which data type is used to store decimal numbers?

A) int
B) char
C) float
D) bool

Answer: C

9. Which symbol is used to end a statement in C++?

A) :
B) .
C) ;
D) ,

Answer: C

10. Which of the following is a valid variable declaration?

A) int 1num;
B) int num1;
C) int num-1;
D) int num 1;

Answer: B

11. Which operator is used for comparison?

A) =
B) ==
C) ++
D) //

Answer: B

12. Which loop is guaranteed to execute at least once?

A) for
B) while
C) do-while
D) nested loop

Answer: C

13. Which keyword is used to define a constant variable?

A) fixed
B) const
C) constant
D) define

Answer: B

14. Which of the following is a correct single-line comment?

A) <!-- comment -->
B) ## comment
C) // comment
D) ** comment

Answer: C

15. Which of the following is a logical operator in C++?

A) &&
B) &
C) %
D) ^

Answer: A


* * * * *

Monday, October 20, 2025

Use of strncmp function in C

strncmp() function

The strncmp function in C is used to compare a specified number of characters from the beginning of two strings. It is defined in the <string.h> header file.

Purpose:
  • Partial String Comparison: strncmp is useful when only a portion of a string needs to be compared, rather than the entire string. This is particularly relevant when dealing with prefixes, command parsing, or verifying specific patterns at the start of a string.
  • Bounded Comparison: It provides a safeguard against buffer overflows by limiting the number of characters compared.
How it works:
The function takes three arguments: two pointers to the strings to be compared (str1 and str2), and an integer n representing the maximum number of characters to compare. It compares characters lexicographically, one by one, from the beginning of each string until "mismatch is found".

Function Syntax
  • int strncmp(const char *str1, const char *str2, size_t n);

Parameters:
  • str1 → first string
  • str2 → second string
  • n → number of characters to compare

Return Value of function:
  • Zero (0): If the first n characters of both strings are equal.
  • Positive value: if first differing character in str1 is greater than that in str2 (difference ASCII value)
  • Negative value: if first differing character in str1 is less than that in str2 (difference of ASCII value)
Example

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Computer";
    char str2[] = "Compute";
   
    // Compare first 7 characters
    int result = strncmp(str1, str2, 7);

    if (result == 0)
        printf("First 7 characters are same.\n");
    else if (result > 0)
        printf("str1 is greater than str2.\n");
    else
        printf("str1 is smaller than str2.\n");

    return 0;
}

Output of program:

First 7 characters are same.