3. Programming Language and Its Applications

Computer Engineering (Nepal Engineering Council) – Engineering Licence Exam

This chapter introduces students to the concept of programming languages and their importance in computer science. It covers the various types of programming paradigms, including procedural, object-oriented, and scripting languages. Students will understand how programming is applied across industries, from web development to automation and data analysis. Through real-life examples and clear explanations, learners gain insight into how programming enables the creation of software, apps, and intelligent systems.

Comprehensive Study Guide for NEC License Exam - Computer Engineering

3. Programming Language and Its Applications (ACtE03)

Introduction

The Nepal Engineering Council (NEC) License Exam is a mandatory requirement for Computer Engineering graduates in Nepal to register as professional engineers. The section 3. Programming Language and Its Applications (ACtE03) tests proficiency in C and C++ programming, focusing on fundamental constructs, object-oriented programming (OOP), file handling, and advanced concepts like templates and exception handling. These skills are critical for software development and system programming. This comprehensive note covers all subtopics (3.1 to 3.6) with detailed explanations, code examples, diagram names, and exam-focused strategies to ensure students can rely on it for thorough preparation. The exam consists of 100 multiple-choice questions (MCQs), with 10 marks allocated to this section, requiring a 50% passing threshold and no negative marking.

The syllabus includes:

  • 3.1 Introduction to C programming (ACtE0301)
  • 3.2 Pointers, structure, and data files in C programming (ACtE0302)
  • 3.3 C++ language constructs with objects and classes (ACtE0303)
  • 3.4 Features of object-oriented programming (ACtE0304)
  • 3.5 Pure virtual function and file handling (ACtE0305)
  • 3.6 Generic programming and exception handling (ACtE0306)

3.1 Introduction to C Programming (ACtE0301)

Key Concepts

C Tokens: Basic building blocks of C programs:

  • Keywords: Reserved words (e.g., int, if, return).
  • Identifiers: Variable/function names (e.g., sum, main).
  • Constants: Fixed values (e.g., 10, 3.14).
  • Operators: Arithmetic (+, -, *, /, %), relational (>, <, ==), logical (&&, ||, !), bitwise (&, |, ^).

Operators: Precedence and associativity govern evaluation (e.g., * > +).

Formatted/Unformatted I/O:

  • Formatted: printf (e.g., printf("%d", x)), scanf (e.g., scanf("%d", &x)).
  • Unformatted: getchar, putchar, gets, puts.

Control Statements:

  • Conditional: if, if-else, switch.
  • Jumps: break, continue, return.

Looping: for, while, do-while (e.g., for(i=0; i<5; i++)).

User-Defined Functions: Modular code with return types and parameters (e.g., int add(int a, int b)).

Recursive Functions: Function calls itself (e.g., factorial: int fact(int n) { if(n==0) return 1; return n * fact(n-1); }).

Arrays:

  • 1-D Array: int arr[5];.
  • 2-D Array: int arr[3][4];.
  • Multi-Dimensional: Higher dimensions (e.g., int arr[2][3][4];).

String Manipulations: Using character arrays or library functions (e.g., strlen, strcpy, strcmp).

Code Example

#include <stdio.h>
int fact(int n) {
    if (n == 0) return 1;
    return n * fact(n-1);
}
int main() {
    int n = 5;
    printf("Factorial of %d is %d\n", n, fact(n));
    return 0;
}
  

Diagram Names

  • C Program Structure Diagram
  • Control Flow Diagram (if-else)
  • 2-D Array Representation Diagram

Common MCQ Areas

  • Output of C programs with loops, conditionals, and functions.
  • String manipulation and array indexing.
  • Operator precedence and recursive function outputs.

Practice Tips

  • Study “The C Programming Language” by Kernighan and Ritchie for fundamentals.
  • Write programs for factorial (recursive), matrix addition, and string operations.
  • Trace outputs of nested loops and conditional statements.
  • Practice using a C compiler (e.g., GCC) to test code.

Sample MCQs

  1. What is the output of: int x = 5; printf("%d", x++);?

    • a) 5
    • b) 6
    • c) 4
    • d) Undefined
    • Answer: a) 5 (Post-increment prints x, then increments).
  2. The function strlen("NEC") returns:

    • a) 3
    • b) 4
    • c) 2
    • d) 0
    • Answer: a) 3 (Counts characters excluding null).

3.2 Pointers, Structure, and Data Files in C Programming (ACtE0302)

Key Concepts

Pointer Arithmetic: Manipulates memory addresses (e.g., p++ increments by data type size).

Pointer and Array: Arrays are pointers to first element (e.g., arr[i] = *(arr + i)).

Passing Pointer to Function: Allows modifying actual parameters (e.g., void swap(int *a, int *b)).

Structure vs. Union:

  • Structure: Allocates memory for all members (e.g., struct { int a; float b; }).
  • Union: Shares memory for all members (e.g., union { int a; float b; }).

Array of Structures: Array of struct objects (e.g., struct student s[10];).

Passing Structure to Function: By value or pointer (e.g., void print(struct student *s)).

Structure and Pointer: Access members using arrow operator (e.g., s->name).

File I/O:

  • Functions: fopen, fclose, fprintf, fscanf.
  • Sequential Access: Read/write in order (e.g., fread).
  • Random Access: Use fseek, ftell.

Code Example

#include <stdio.h>
struct student {
    char name[20];
    int roll;
};
void print(struct student *s) {
    printf("Name: %s, Roll: %d\n", s->name, s->roll);
}
int main() {
    struct student s = {"Ram", 101};
    print(&s);
    return 0;
}
  

Diagram Names

  • Pointer Arithmetic Diagram
  • Structure Memory Layout Diagram
  • File I/O Flowchart

Common MCQ Areas

  • Pointer arithmetic and array-pointer equivalence.
  • Structure vs. union memory allocation.
  • File I/O operations and modes.

Practice Tips

  • Study “The C Programming Language” by Kernighan and Ritchie for pointers and files.
  • Write programs using pointers for array traversal and swapping.
  • Create structures and unions, and practice file read/write operations.
  • Test file I/O with sequential and random access using a C compiler.

Sample MCQs

  1. If int *p = arr; p++; where arr is an array, p points to:

    • a) First element
    • b) Second element
    • c) Last element
    • d) Null
    • Answer: b) Second element (Increments by sizeof(int)).
  2. In fopen("data.txt", "r"), the mode r indicates:

    • a) Write
    • b) Read
    • c) Append
    • d) Read and write
    • Answer: b) Read.

3.3 C++ Language Constructs with Objects and Classes (ACtE0303)

Key Concepts

Namespace: Avoids name conflicts (e.g., namespace std;).

Function Overloading: Multiple functions with same name, different parameters (e.g., int add(int, int); and double add(double, double);).

Inline Functions: Reduces function call overhead (e.g., inline int square(int x) { return x * x; }).

Default Arguments: Optional parameters (e.g., int func(int x, int y=10)).

Pass/Return by Reference: Uses & to modify/pass variables (e.g., void swap(int &a, int &b)).

Class and Object: Class defines blueprint, object is instance (e.g., class Student { int roll; }; Student s;).

Access Specifiers: public, private, protected.

Member Function: Defined inside/outside class (e.g., void Student::display()).

Constructor: Initializes object (default, parameterized, copy).

Destructor: Cleans up resources (e.g., ~Student()).

Dynamic Memory Allocation: new/delete for objects/arrays.

this Pointer: Refers to current object.

Static Members: Shared by all objects (e.g., static int count;).

Constant Members: const functions/objects prevent modification.

Friend Function/Class: Access private members (e.g., friend void print(Student);).

Code Example

#include <iostream>
using namespace std;
class Student {
public:
    int roll;
    Student(int r = 0) : roll(r) {}
    void display() { cout << "Roll: " << roll << endl; }
};
int main() {
    Student s(101);
    s.display();
    return 0;
}
  

Diagram Names

  • Class and Object Diagram
  • Access Specifiers Diagram
  • Constructor Call Flowchart

Common MCQ Areas

  • Output of function overloading and default arguments.
  • Behavior of constructors, destructors, and static members.
  • Access specifiers and friend functions.

Practice Tips

  • Study “C++ Primer” by Lippman for OOP concepts.
  • Write classes with constructors, static members, and friend functions.
  • Practice dynamic memory allocation with new/delete.
  • Test programs using a C++ compiler (e.g., G++).

Sample MCQs

  1. A private member of a class is accessible in:

    • a) Main function
    • b) Friend function
    • c) External function
    • d) None
    • Answer: b) Friend function.
  2. The this pointer refers to:

    • a) Class definition
    • b) Current object
    • c) Static member
    • d) Constructor
    • Answer: b) Current object.

3.4 Features of Object-Oriented Programming (ACtE0304)

Key Concepts

Operator Overloading: Redefines operators (e.g., + for objects):

  • Unary: ++, --.
  • Binary: +, -, ==.

Data Conversion: Type casting (e.g., class to basic type, class to class).

Inheritance: Reuses code from base class:

  • Single: One base class.
  • Multiple: Multiple base classes.
  • Multilevel: Chain of inheritance.
  • Hybrid: Combines multiple types.
  • Multipath: Involves diamond problem (solved by virtual inheritance).

Constructor/Destructor in Inheritance: Base class constructor called before derived; destructor in reverse order.

Code Example

#include <iostream>
using namespace std;
class Base {
public:
    Base() { cout << "Base Constructor\n"; }
};
class Derived : public Base {
public:
    Derived() { cout << "Derived Constructor\n"; }
};
int main() {
    Derived d;
    return 0;
}
  

Diagram Names

  • Inheritance Hierarchy Diagram
  • Operator Overloading Flowchart
  • Diamond Problem Diagram

Common MCQ Areas

  • Output of programs with inheritance and constructor calls.
  • Operator overloading syntax and behavior.
  • Resolving ambiguity in multiple inheritance.

Practice Tips

  • Study “C++ Primer” by Lippman for OOP features.
  • Implement unary/binary operator overloading for a class.
  • Create single and multiple inheritance examples, tracing constructor/destructor calls.
  • Test programs with virtual inheritance to resolve diamond problem.

Sample MCQs

  1. In multiple inheritance, constructor execution order is:

    • a) Derived first
    • b) Base classes first
    • c) Random order
    • d) No constructors
    • Answer: b) Base classes first.
  2. Virtual inheritance is used to resolve:

    • a) Function overloading
    • b) Diamond problem
    • c) Operator overloading
    • d) Constructor ambiguity
    • Answer: b) Diamond problem.

3.5 Pure Virtual Function and File Handling (ACtE0305)

Key Concepts

Virtual Function: Enables runtime polymorphism (e.g., virtual void display()).

Pure Virtual Function: virtual void func() = 0; makes class abstract.

Dynamic Binding: Resolves function calls at runtime via virtual table (vtable).

File Handling:

  • Opening/Closing: ifstream, ofstream, close().
  • I/O Operations: >> (input), << (output).
  • Error Handling: eof(), fail().

Stream Class Hierarchy: iosistream, ostream, iostream.

Unformatted I/O: get, put.

Formatted I/O: ios flags (e.g., ios::fixed), manipulators (e.g., setw, setprecision).

Code Example

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ofstream out("data.txt");
    out << "NEC Exam";
    out.close();
    return 0;
}
  

Diagram Names

  • Virtual Function Vtable Diagram
  • Stream Class Hierarchy Diagram
  • File I/O Flowchart

Common MCQ Areas

  • Role of pure virtual functions in abstract classes.
  • File stream operations and error handling.
  • Formatted I/O with manipulators and flags.

Practice Tips

  • Study “C++ Primer” by Lippman for virtual functions and file handling.
  • Implement abstract classes with pure virtual functions.
  • Write file I/O programs for reading/writing text and binary files.
  • Practice formatting output with setw and setprecision.

Sample MCQs

  1. A pure virtual function:

    • a) Has a body
    • b) Makes class abstract
    • c) Cannot be overridden
    • d) Is static
    • Answer: b) Makes class abstract.
  2. The manipulator setw(5) affects:

    • a) Precision
    • b) Width
    • c) Base
    • d) Alignment
    • Answer: b) Width.

3.6 Generic Programming and Exception Handling (ACtE0306)

Key Concepts

Function Template: Generic function for multiple types (e.g., template <typename T> T max(T a, T b)).

Overloading Function Template: Specialized versions for specific types.

Class Template: Generic class (e.g., template <class T> class Stack).

Standard Template Library (STL):

  • Containers: vector, list, map.
  • Algorithms: sort, find.
  • Iterators: Pointer-like objects for traversing containers.

Exception Handling:

  • Constructs: try, catch, throw.
  • Multiple Exceptions: Multiple catch blocks.
  • Rethrowing: Re-throw exception in catch.
  • Catching All: catch(...).
  • Exception Specification: Restrict thrown types (deprecated in C++11).

Code Example

#include <iostream>
using namespace std;
template <typename T>
T max(T a, T b) { return (a > b) ? a : b; }
int main() {
    try {
        if (max(5, 10) != 10) throw "Error";
        cout << "Max: " << max(5, 10) << endl;
    } catch (const char* msg) {
        cout << msg << endl;
    }
    return 0;
}
  

Diagram Names

  • Template Function Flowchart
  • STL Container Hierarchy Diagram
  • Exception Handling Flowchart

Common MCQ Areas

  • Output of template functions and classes.
  • Exception handling constructs and catch block behavior.
  • STL container and algorithm usage.

Practice Tips

  • Study “C++ Primer” by Lippman for templates and STL.
  • Write function/class templates for operations like sorting or stacking.
  • Implement exception handling with multiple catch blocks.
  • Use STL vector and sort in programs.

Sample MCQs

  1. A function template is defined using:

    • a) virtual
    • b) template
    • c) inline
    • d) static
    • Answer: b) template.
  2. The catch(...) block handles:

    • a) Specific exceptions
    • b) All exceptions
    • c) No exceptions
    • d) Only integers
    • Answer: b) All exceptions.

General Preparation Strategies

  • Syllabus Review: Study the official NEC syllabus to focus on key programming concepts.
  • Textbooks: Use “The C Programming Language” by Kernighan and Ritchie for C, and “C++ Primer” by Lippman for C++ and OOP.
  • Practice: Write 20–30 programs per subtopic, focusing on loops, pointers, classes, and file handling.
  • Simulations: Use compilers like GCC or G++ to test C/C++ programs and verify outputs.
  • Time Management: Practice 10–15 MCQs in 12–15 minutes to match exam pace.

Conclusion

This comprehensive study guide covers all subtopics under 3. Programming Language and Its Applications (ACtE03) for the NEC License Exam, providing detailed explanations, code examples, diagram names, and practice strategies. Designed as a reliable resource for students, it ensures thorough preparation for C and C++ programming concepts. By mastering these topics and practicing MCQs, candidates can confidently excel in the exam.