logo

3. Programming Language and Its Applications

Computer Engineering - Nec (Nepal Engineering Council)

MCQ questions

3. Programming Language and Its Applications (ACtE03)

3.1 Introduction to C Programming

C Tokens

  • Definition: The smallest individual units in a C program.
  • Types:
    • Keywords: int, return, if, else, etc.
    • Identifiers: Variable and function names.
    • Constants: Numeric or character values (10, 'A', 3.14).
    • Strings: "Hello, World!"
    • Operators: +, -, *, /, ==, etc.
    • Special Symbols: {}, (), [], ,, ;, etc.

Operators

  • Arithmetic: +, -, *, /, %
  • Relational: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !
  • Bitwise: &, |, ^, ~, <<, >>
  • Assignment: =, +=, -=, *=, /=, %=
  • Increment/Decrement: ++, --
  • Ternary: condition ? value1 : value2

Formatted and Unformatted Input/Output

  • Formatted I/O: Uses printf() and scanf().
    c
    printf("Enter a number: "); scanf("%d", &num);
  • Unformatted I/O: Uses getchar(), putchar(), gets(), puts().
    c
    char ch = getchar(); putchar(ch);

Control Statements

  • Conditional Statements: if, if-else, switch-case
    c
    if (num > 0) { printf("Positive number"); } else { printf("Non-positive number"); }
  • Looping Statements: for, while, do-while
    c
    for(int i=0; i<5; i++) { printf("%d\n", i); }

User-defined Functions

  • Functions help in modular programming.
    c
    int add(int a, int b) { return a + b; }

Recursive Functions

  • Functions that call themselves.
    c
    int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); }

Arrays

  • 1-D Array:
    c
    int arr[5] = {1, 2, 3, 4, 5};
  • 2-D Array:
    c
    int matrix[2][2] = {{1, 2}, {3, 4}};
  • Multi-Dimensional Array: Arrays with more than two dimensions.

String Manipulations

  • String Declaration:
    c
    char str[] = "Hello";
  • String Functions (string.h):
    c
    strcpy(dest, src); strcat(s1, s2); strlen(str); strcmp(s1, s2);

3.2 Pointers, Structure, and Data Files in C Programming

Pointer Arithmetic

  • A pointer stores the address of a variable.
    c
    int a = 10; int *ptr = &a;

Pointer and Array

  • A pointer can traverse an array.
    c
    int arr[3] = {1, 2, 3}; int *ptr = arr;

Passing Pointer to Function

  • Used for efficient memory handling.
    c
    void increment(int *x) { (*x)++; }

Structure vs Union

  • Structure: Stores different data types.
    c
    struct Student { int id; char name[20]; };
  • Union: Shares memory for all variables.
    c
    union Data { int x; float y; };

File Handling in C

  • Opening a File:
    c
    FILE *fp = fopen("file.txt", "r");
  • Reading from File:
    c
    fscanf(fp, "%s", str);
  • Writing to File:
    c
    fprintf(fp, "Hello");
  • Closing File:
    c
    fclose(fp);

3.3 C++ Language Constructs with Objects and Classes

Namespace

  • Avoids name conflicts.
    cpp
    namespace myNamespace { int x = 10; }

Function Overloading

  • Multiple functions with the same name but different parameters.
    cpp
    void display(int a); void display(float a);

Classes and Objects

  • Defining a Class:
    cpp
    class Car { public: string brand; };
  • Creating an Object:
    cpp
    Car myCar;

Constructor and Destructor

  • Constructor: Initializes an object.
    cpp
    class MyClass { public: MyClass() { cout << "Object Created"; } };
  • Destructor: Cleans up resources.
    cpp
    ~MyClass() { cout << "Object Destroyed"; }

3.4 Features of Object-Oriented Programming

  • Operator Overloading:
    cpp
    Complex operator+(const Complex &c);
  • Inheritance:
    cpp
    class Child : public Parent {};

3.5 Pure Virtual Function and File Handling

Virtual Function & Dynamic Binding

  • Enables runtime polymorphism.
    cpp
    class Base { virtual void show(); };

File Handling in C++

  • Opening File:
    cpp
    ofstream file("test.txt");
  • Reading File:
    cpp
    ifstream file("test.txt");

3.6 Generic Programming and Exception Handling

Function Template

  • Creates generic functions.
    cpp
    template <typename T> T add(T a, T b) { return a + b; }

Class Template

  • Allows generic data types.
    cpp
    template <class T> class Box { T data; };

Exception Handling

  • Basic Syntax:
    cpp
    try { throw 10; } catch (int x) { cout << "Exception: " << x; }