Skip to main content

Chapter 5

Programming Concepts and Logics

NEB Class 11 past exam questions

Past questions and answers

59 questions set from this chapter, 12 of them more than once; 2 are most repeated (set, or a close variant set, in 3 or more exams). Most repeated first.

  • Most repeated · 3 of 29 exams
  • Asked 2 times
  • 2078 Second Term · Science Morning · Trinity · 8 marks
  • 2078 Third Term · Science Day · Trinity · 5 marks

Write a C program to check whether a number given by the user is a palindrome or not.

Similar questions: Divisible by 5 (2078 Second Term · Science Morning · Trinity)

Answer

Logic: A number is a palindrome if it reads the same forwards and backwards (e.g. 121). Reverse the number by repeatedly taking the last digit (n % 10) and building the reverse (rev = rev*10 + digit), then compare it with the original.

#include <stdio.h>

int main()
{
    int n, original, rev = 0, digit;

    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    while (n > 0)
    {
        digit = n % 10;
        rev = rev * 10 + digit;
        n = n / 10;
    }

    if (original == rev)
        printf("%d is a palindrome", original);
    else
        printf("%d is not a palindrome", original);

    return 0;
}

Dry run for 121: digit 1, rev = 1; digit 2, rev = 12; digit 1, rev = 121. Since 121=121121 = 121, it is a palindrome. For 123 the reverse is 321, so it is not a palindrome.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Most repeated · 3 of 29 exams
  • Asked 2 times
  • 2078 Second Term · Science Morning · Trinity · 5 marks
  • 2078 Second Term · Management Day · Trinity · 5 marks

Write a C program to check whether a number given by the user is exactly divisible by 5 or not.

Similar questions: Palindrome number (2078 Second Term · Science Morning · Trinity)

Answer

Logic: A number is exactly divisible by 5 if the remainder on dividing by 5 is zero, i.e. n % 5 == 0.

#include <stdio.h>

int main()
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n % 5 == 0)
        printf("%d is exactly divisible by 5", n);
    else
        printf("%d is not divisible by 5", n);

    return 0;
}

Sample run: input 35 gives 35 is exactly divisible by 5, because 35 mod 5=035 \bmod 5 = 0. Input 22 gives 22 is not divisible by 5, because 22 mod 5=222 \bmod 5 = 2.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Asked 2 times
  • 2078 Annual · Trinity (Management) · 1 mark
  • 2078 Model Set E · 1 mark

What are the major components of a selection structure? (a) condition (b) true statement (c) false statement (d) all

Answer

(d) all

A selection structure is made of a condition that is tested, a statement block executed when the condition is true, and a statement block executed when it is false.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • Asked 2 times
  • 2078 Model Set C · 1 mark
  • 2078 Third Term · Science Morning · Trinity · 1 mark

'==' is called ..... operator? (a) relational (b) unary (c) equals to (d) a and c

Answer

(d) a and c

The == operator compares two values and is a relational operator, also read as the "equal to" operator. So both (a) and (c) are correct.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • Asked 2 times
  • 2078 Model Set D · 1 mark
  • 2079 Second Term · Management Morning · 1 mark

Which is a valid identifier/variable name in C? (a) int a1; (b) int a-1; (c) int 1a; (d) int a b -1;

Answer

(a) int a1;

An identifier may contain letters, digits and underscore, and must not begin with a digit. a1 is valid, a-1 contains a minus sign, 1a starts with a digit, and a b -1 has a space and a minus sign.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • Asked 2 times
  • 2079 Second Term · Science Morning · 1 mark
  • 2079 Second Term · Management Day · 1 mark

The format specifier for string is (a) %d (b) %f (c) %s (d) %c

Answer

(c) %s

%s is used for strings (character arrays). %d is for int, %f for float and %c for a single character.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • Asked 2 times
  • 2078 Second Term · Management Morning · Trinity · 1 mark
  • 2078 Second Term · Management Day · Trinity · 1 mark

Which of the following is a mandatory function in C programming? (a) main (b) printf (c) scanf (d) None of above

Answer

(a) main

Every C program must contain a main() function because execution begins there. printf and scanf are optional library functions.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • Asked 2 times
  • 2078 Annual · Trinity (Science) · 5 marks
  • 2078 Model Set E · 5 marks

Define the term 'loop'. Explain the 'for' loop.

Answer

Loop: A loop is a control structure that repeats a statement or a block of statements again and again as long as a given condition remains true. It saves the programmer from writing the same code many times. C provides for, while and do...while loops.

The for loop: The for loop is an entry-controlled loop. It is used when the number of repetitions is known in advance. Initialization, condition and increment/decrement are all written in one line.

Syntax:

for (initialization; condition; update)
{
    statement(s);
}

Working:

  1. The initialization is executed once, at the beginning.
  2. The condition is tested. If it is true, the body is executed; if it is false, the loop ends.
  3. After the body, the update expression (such as i++) is executed, and control returns to step 2.

Example: printing the numbers 1 to 5.

#include <stdio.h>
int main()
{
    int i;
    for (i = 1; i <= 5; i++)
    {
        printf("%d\n", i);
    }
    return 0;
}

Output: 1 2 3 4 5 (each on a new line). Here i=1 is the initialization, i<=5 is the condition and i++ is the update.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Asked 2 times
  • 2079 Second Term · Management Day · 1+3 marks
  • 2078 Second Term · Management Morning · Trinity · 3 marks

What is an operator in C? Explain about arithmetic operators.

Answer

Operator: An operator is a symbol that tells the compiler to perform a particular operation on operands.

Arithmetic operators: These perform basic mathematical calculations.

OperatorOperationExample (a = 10, b = 3)Result
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3 (integer division)
%Modulus (remainder)a % b1

Notes: for two integers / gives the integer quotient (10/3=310/3=3), while % works only with integers and gives the remainder (10 mod 3=110 \bmod 3 = 1). Increment ++ and decrement -- are also arithmetic operators that add or subtract 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Asked 2 times
  • 2078 Annual · Trinity (Management) · 8 marks
  • 2078 Model Set E · 8 marks

Write a program to subtract two 2×22 \times 2 matrices.

Answer

Logic: Subtract the corresponding elements of the two matrices: Cij=Aij−BijC_{ij} = A_{ij} - B_{ij} for every row ii and column jj. Nested loops are used to read, subtract and display the 2×22 \times 2 matrices.

#include <stdio.h>

int main()
{
    int a[2][2], b[2][2], c[2][2];
    int i, j;

    printf("Enter elements of first matrix:\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &a[i][j]);

    printf("Enter elements of second matrix:\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &b[i][j]);

    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            c[i][j] = a[i][j] - b[i][j];

    printf("Difference of the matrices:\n");
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
            printf("%d\t", c[i][j]);
        printf("\n");
    }
    return 0;
}

Sample run: for A=[5869]A=\begin{bmatrix}5&8\\6&9\end{bmatrix} and B=[1234]B=\begin{bmatrix}1&2\\3&4\end{bmatrix}, the result is A−B=[4635]A-B=\begin{bmatrix}4&6\\3&5\end{bmatrix}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Asked 2 times
  • 2078 Second Term · Management Morning · Trinity · 5 marks
  • 2079 Second Term · Science Morning · 5 marks

Write a program in C to check whether the given number is odd or even.

Answer

Logic: A number is even if it is exactly divisible by 2 (remainder 0); otherwise it is odd.

#include <stdio.h>

int main()
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n % 2 == 0)
        printf("%d is even", n);
    else
        printf("%d is odd", n);

    return 0;
}

Sample run: input 8 gives 8 is even since 8 mod 2=08 \bmod 2 = 0; input 7 gives 7 is odd since 7 mod 2=17 \bmod 2 = 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Asked 2 times
  • 2078 Second Term · Management Morning · Trinity · 3+5 marks
  • 2078 Second Term · Management Day · Trinity · 3+5 marks

What is an arithmetic operator in C? Write a C program to find the area and perimeter of a rectangle.

Answer

Arithmetic operator: An arithmetic operator is a symbol that tells the compiler to perform a mathematical calculation on operands. The arithmetic operators in C are + (addition), - (subtraction), * (multiplication), / (division) and % (modulus, the remainder after integer division).

Program to find the area and perimeter of a rectangle (area =l×b= l \times b, perimeter =2(l+b)= 2(l + b)):

#include <stdio.h>
#include <conio.h>

void main()
{
    float l, b, area, perimeter;
    clrscr();
    printf("Enter length and breadth: ");
    scanf("%f%f", &l, &b);
    area = l * b;
    perimeter = 2 * (l + b);
    printf("Area = %.2f\n", area);
    printf("Perimeter = %.2f\n", perimeter);
    getch();
}

For example, if l=5l = 5 and b=3b = 3, the program prints Area = 15.00 and Perimeter = 16.00.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • CDC Class 11 Computer Science Model Question · 1 mark
  • 2078 Model Set A · 1 mark
  • Trinity Class 11 Computer Science Third Term · 1 mark

Which looping process checks the test condition at the end of the loop? (a) for (b) while (c) do-while (d) Nested loop

Similar questions: Loop test at beginning (2078 Model Set B)

Answer

(c) do-while

A do-while loop checks its test condition at the end of each iteration, so its body executes at least once before the condition is evaluated.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Model Set B · 1 mark

Which looping process checks the test condition at the beginning of the loop? (a) for (b) while (c) do-while (d) loop

Similar questions: Which looping process checks the test condition (2078 Model Set A)

Answer

(b) while

A while loop is an entry-controlled loop: its condition is tested before the body is executed, so the body may not execute at all. The do-while loop tests at the end.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Management Morning · Trinity · 1 mark

Which of the following is the type specifier of float? (a) %d (b) %c (c) %f (d) All of above

Similar questions: Format specifier of int (2078 Second Term · Management Day · Trinity)

Answer

(c) %f

%f is the format specifier for float values. %d is for integers and %c for characters.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Management Day · Trinity · 1 mark

Which of the following is the type specifier of int? (a) %d (b) %c (c) %f (d) All of above

Similar questions: Format specifier of float (2078 Second Term · Management Morning · Trinity)

Answer

(a) %d

%d is the format specifier for int values. %c is for characters and %f for floats.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Management Morning · Trinity · 1 mark

What is the use of printf()? (a) input (b) output (c) read (d) None

Similar questions: Use of scanf() (2078 Second Term · Management Day · Trinity)

Answer

(b) output

The printf() function displays text and values on the screen, so it is used for output.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Management Day · Trinity · 1 mark

What is the use of scanf()? (a) input (b) output (c) read (d) None

Similar questions: Use of printf() (2078 Second Term · Management Morning · Trinity)

Answer

(a) input

The scanf() function takes data from the keyboard and stores it in variables, so it is used for input.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • CDC Class 11 Computer Science Model Question · 5 marks
  • 2078 Model Set A · 5 marks

Differentiate between the do and while loop.

Answer

Difference between do...while loop and while loop

do...while loopwhile loop
It is an exit-controlled (post-test) loop.It is an entry-controlled (pre-test) loop.
The test condition is checked after executing the body of the loop.The test condition is checked before executing the body of the loop.
The body executes at least once, even if the condition is false initially.The body may execute zero times if the condition is false initially.
Syntax: do { statements; } while (condition);Syntax: while (condition) { statements; }
A semicolon (;) is required after the while (condition).No semicolon is used after the condition.
It is less commonly used; suitable when the body must run at least once.It is more commonly used; suitable when the number of repetitions is not fixed.

Example (do...while):

int i = 1;
do {
    printf("%d ", i);
    i++;
} while (i <= 5);   // prints 1 2 3 4 5

Example (while):

int i = 1;
while (i <= 5) {
    printf("%d ", i);
    i++;
}   // prints 1 2 3 4 5

If the condition is initially false (say i = 10), the do...while loop still prints 10 once, whereas the while loop prints nothing.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • CDC Class 11 Computer Science Model Question · 8 marks
  • 2078 Model Set A · 8 marks

Write a program to input the elements of 4 x 3 matrix and prints its elements properly using array.

Answer

Program to Input and Print a 4 × 3 Matrix Using an Array

Concept used: A two-dimensional array (matrix) of size 4 rows × 3 columns. Nested loops are used — the outer loop controls the rows and the inner loop controls the columns. The same nested-loop structure is used first to read the elements and then to display them in proper row-and-column form.

C Program

#include <stdio.h>

int main()
{
    int a[4][3];          /* 2D array: 4 rows, 3 columns */
    int i, j;

    /* Reading the elements of the matrix */
    printf("Enter the elements of 4 x 3 matrix:\n");
    for (i = 0; i < 4; i++)          /* outer loop for rows */
    {
        for (j = 0; j < 3; j++)      /* inner loop for columns */
        {
            scanf("%d", &a[i][j]);
        }
    }

    /* Printing the elements in proper matrix form */
    printf("\nThe entered matrix is:\n");
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 3; j++)
        {
            printf("%d\t", a[i][j]);   /* \t keeps columns aligned */
        }
        printf("\n");                  /* new line after each row */
    }

    return 0;
}

Sample Output

Enter the elements of 4 x 3 matrix:
1 2 3
4 5 6
7 8 9
10 11 12

The entered matrix is:
1       2       3
4       5       6
7       8       9
10      11      12

Explanation

  1. int a[4][3]; declares a two-dimensional array that can hold 4 × 3 = 12 integer elements. The first index denotes the row and the second index denotes the column.
  2. The first pair of nested for loops reads the elements. i runs from 0 to 3 (rows) and j runs from 0 to 2 (columns), so &a[i][j] accepts each element one by one.
  3. The second pair of nested for loops prints the elements. The inner loop prints all three elements of one row, and the printf("\n") after the inner loop moves the cursor to the next line, so the output appears in proper matrix form.
  4. The \t (tab escape sequence) inserts spacing between the elements so that the columns line up neatly.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · NEB Old Course 130 'B' · 5 marks

Differentiate between compiler and interpreter.

Answer

Both compilers and interpreters are language translators that convert a high-level language program into machine language, but they work differently.

BasisCompilerInterpreter
TranslationTranslates the whole program at onceTranslates and executes one statement at a time
OutputProduces an object/executable fileProduces no separate object file
Speed of executionFast, because the translated program is run directlySlower, because each line is translated every time it runs
Error reportingShows all errors together after compilationStops and shows an error at the first faulty statement
DebuggingHarder, since errors appear in a list at the endEasier, since errors are found line by line
MemoryNeeds more memory for the object codeNeeds less memory
Re-translationNot needed once compiled; the program can run repeatedlyNeeded every time the program is run
ExamplesC, C++, Java (compiler)BASIC, Python, JavaScript

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · NEB Old Course 130 'B' · 2+8 marks

What is a programming language? Describe the various types of programming language with their merits and demerits.

Answer

Programming language: A programming language is a set of rules, words and symbols (syntax) used to write instructions that tell a computer what to do. Programmers use it to develop software.

Types of programming language:

1. Machine language (low-level, first generation)

Instructions are written in binary form (0s and 1s), which the computer understands directly.

  • Merits: very fast execution because no translator is needed; efficient use of memory.
  • Demerits: very hard to write, read and debug; machine dependent (not portable); errors are difficult to find.

2. Assembly language (low-level, second generation)

Uses short symbolic codes called mnemonics (such as ADD, MOV) instead of binary. An assembler translates it into machine language.

  • Merits: easier than machine language; faster and uses less memory than high-level languages; gives control over hardware.
  • Demerits: still machine dependent; requires knowledge of hardware; difficult for large programs; needs an assembler.

3. High-level language (third generation)

Uses English-like words and mathematical notation, e.g. C, C++, Java, Python, BASIC. A compiler or interpreter translates it into machine language.

  • Merits: easy to learn, write and debug; machine independent (portable); fewer lines of code; suited to large programs.
  • Demerits: slower than low-level languages because translation is needed; uses more memory; less direct control of hardware.

4. Fourth-generation languages (4GL)

Very high-level, often non-procedural languages used for databases and reports, e.g. SQL.

  • Merits: the programmer states what is needed, not how to do it; very fast development; requires very little coding.
  • Demerits: less flexible; executes slowly and needs more resources; mostly limited to specific tasks.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Annual · Trinity (Science) · 1 mark

Which term describes an error in the sequence of characters or tokens that is intended to be written in a particular programming language? (a) Syntax Error (b) Semantic Error (c) Runtime Error (d) Linker Error

Answer

(a) Syntax Error

A syntax error is a violation of the grammar rules of the language, for example a missing semicolon or a wrongly written keyword. The compiler reports it during compilation.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Science Morning · Trinity · 1 mark

Valid identifiers cannot start with ... (a) digit (b) underscore (c) uppercase (d) lowercase

Answer

(a) digit

In C an identifier must begin with a letter or an underscore. It cannot start with a digit.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Science Morning · Trinity · 1 mark

Which header file is required to execute getch() in a program? (a) conio.h (b) stdio.h (c) stdlib.h (d) string.h

Answer

(a) conio.h

The function getch() is declared in the console input/output header file conio.h.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Science Morning · Trinity · 1 mark

Which of the following is a problem oriented language? (a) BASIC (b) C Language (c) FORTRAN (d) C# Language

Answer

(c) FORTRAN

Problem-oriented (high-level) languages are designed to solve particular kinds of problems and use English-like statements. FORTRAN (Formula Translation) was designed for scientific and mathematical problems, which makes it the classic example.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Science Day · Trinity · 1 mark

Which operator is used to find the remainder of two integers? (a) / (b) <> (c) MOD (d) %

Answer

(d) %

The modulus operator % gives the remainder of integer division. For example, 7 % 3 gives 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Second Term · Science Day · Trinity · 1 mark

Which component is not required to define a simple loop? (a) Initialization (b) condition (c) counter (d) break

Answer

(d) break

A simple loop needs initialization, a condition and a counter update. The break statement is optional; it is only used to leave a loop early.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2079 Second Term · Science Morning · 1 mark

What would be the output?
int main()
{
int a=5,b=6;
printf("%d",(a>b)+1);
return 0;
}
(a) 56 (b) 65 (c) 0 (d) 1

Answer

(d) 1

Since a=5a=5 and b=6b=6, the expression a>ba>b is false and has the value 0. Then 0+1=10+1=1, so the program prints 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2079 Second Term · Science Morning · 1 mark

The program written by us, when compiled, is converted into (a) assembler language (b) machine language (c) english language (d) nepali language

Answer

(b) machine language

A compiler translates the source code written by the programmer into machine language (binary), which the computer can execute directly.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2079 Second Term · Science Day · 1 mark

Every code execution begins from which of the following functions? (a) main() (b) User defined function (c) getchar() (d) printf()

Answer

(a) main()

Execution of every C program starts from the main() function, whatever its position in the file.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2079 Second Term · Management Morning · 1 mark

Which of the following is an input function? (a) printf() (b) scanf() (c) main() (d) input()

Answer

(b) scanf()

scanf() reads input from the keyboard. printf() is for output, main() is the starting function, and input() is not a C function.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2079 Second Term · Management Morning · 1 mark

Which of the following is not a standard header file in C? (a) stdlib.h (b) stdio.h (c) string.h (d) maths.h

Answer

(d) maths.h

The standard C header for mathematical functions is math.h, not maths.h. The headers stdlib.h, stdio.h and string.h are all standard.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2079 Second Term · Management Day · 1 mark

What is the use of gets() in C language? (a) input (b) output (c) to store in file (d) to delete data

Answer

(a) input

gets() reads a line of text (a string, including spaces) from the keyboard into a character array, so it is an input function.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2079 Second Term · Management Day · 1 mark

What would be the output of the following C code?
int a=78;
a=a+5;
printf("%d",a-5);
(a) 83 (b) 78 (c) 80 (d) 90

Answer

(b) 78

After a=a+5; the value of a becomes 83. The statement printf("%d", a-5); prints 83−5=7883-5=78 (the value of a itself is not changed by this expression).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Third Term · Science Day · Trinity · 1 mark

What is the output of the statement printf("%d", (a++))? (a) The value of (a + 1) (b) The current value of a (c) Error message (d) Garbage

Answer

(b) The current value of a

a++ is the post-increment operator. It first uses the current value of a (which printf prints) and only afterwards increases a by 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • 2078 Annual · Trinity (Management) · 5 marks

Differentiate between FOR and DO WHILE loop.

Answer

Basisfor loopdo...while loop
TypeEntry-controlled loop (condition is checked before the body)Exit-controlled loop (condition is checked after the body)
Minimum executionsBody may not execute at all if the condition is false at firstBody executes at least once, even if the condition is false
Syntaxfor(init; condition; update){ ... }do{ ... }while(condition);
Initialization, condition and updateAll three are written together in the loop headerInitialization is written before the loop and update inside the body
SemicolonNo semicolon after the closing bracket of the headerA semicolon is required after while(condition)
Best used whenThe number of repetitions is knownThe number of repetitions is not known and the body must run at least once (e.g. menus)

Example of for: for(i=1;i<=3;i++) printf("%d ",i);

Example of do...while: i=1; do{ printf("%d ",i); i++; }while(i<=3);

Both print 1 2 3, but only do...while would still print once if the condition were false initially.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Model Set B · 2+3 marks
  • Trinity Class 11 Computer Science Third Term · 2+3 marks

What is a nested loop? Differentiate between the do and while loop.

Answer

Nested loop: A loop written inside the body of another loop is called a nested loop. The inner loop completes all its repetitions for each single repetition of the outer loop. For example, if the outer loop runs 3 times and the inner loop runs 4 times, the inner body runs 3×4=123 \times 4 = 12 times.

for (i = 1; i <= 3; i++)
    for (j = 1; j <= 4; j++)
        printf("*");

Difference between while and do...while loop:

Basiswhile loopdo...while loop
Condition checkChecked before the body (entry-controlled)Checked after the body (exit-controlled)
Minimum executionsMay execute zero timesExecutes at least once
Syntaxwhile(condition){ ... }do{ ... }while(condition);
SemicolonNot used after the conditionUsed after while(condition)
UseWhen the body should run only if the condition is trueWhen the body must run at least once

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Third Term · Science Morning · Trinity · 2+3 marks

What is a nested loop? Explain the FOR loop with an appropriate example.

Answer

Nested loop: A loop placed inside the body of another loop is called a nested loop. For every single repetition of the outer loop, the inner loop runs completely. If the outer loop runs mm times and the inner loop nn times, the inner body runs m×nm \times n times.

The for loop: The for loop repeats a block of statements a fixed number of times. It is an entry-controlled loop with initialization, condition and update written together.

for (initialization; condition; update)
{
    statement(s);
}

The initialization runs once, then the condition is tested before each pass; if it is true the body runs and then the update is executed, otherwise the loop ends.

Example: displaying the sum of the first 5 natural numbers.

#include <stdio.h>
int main()
{
    int i, sum = 0;
    for (i = 1; i <= 5; i++)
    {
        sum = sum + i;
    }
    printf("Sum = %d", sum);
    return 0;
}

Output: Sum = 15, because 1+2+3+4+5=151+2+3+4+5=15.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Second Term · Science Day · Trinity · 5 marks

What is pseudo code? Explain its importance in programming.

Answer

Pseudo code: Pseudo code is an informal, step-by-step description of an algorithm written in plain English-like statements that resemble a programming language. It does not follow the strict syntax of any language and cannot be run by a computer; it is only used to plan the logic of a program.

Example: to find the largest of two numbers.

START
INPUT a, b
IF a > b THEN
    PRINT a
ELSE
    PRINT b
END IF
STOP

Importance in programming:

  • Easy to understand: it is written in simple language, so even a person who does not know a particular programming language can follow the logic.
  • Language independent: the same pseudo code can be converted into C, Python, Java or any other language.
  • Helps in planning: the logic is designed and checked before coding, which reduces logical errors.
  • Easy to convert into code: each line can be translated directly into program statements.
  • Saves time and cost: errors are corrected at the design stage, which is cheaper than correcting a finished program.
  • Good documentation: it helps teams to communicate the design and helps in later maintenance.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Model Set C · 1+4 marks

What is an operator? Explain about the ternary and sizeof() operator.

Answer

Operator: An operator is a symbol that tells the compiler to perform a specific operation (mathematical, logical or relational) on one or more values called operands. Examples are +, -, >, &&.

Ternary (conditional) operator ?:: It is the only C operator that takes three operands. It works like a short if...else statement.

Syntax: condition ? expression1 : expression2;

If the condition is true, expression1 is evaluated; otherwise expression2 is evaluated.

int a = 10, b = 20, max;
max = (a > b) ? a : b;
printf("%d", max);

Output: 20, because a > b is false, so b is chosen.

sizeof() operator: It is a unary operator that returns the number of bytes of memory occupied by a data type or a variable. The result is an unsigned integer.

int x;
printf("%d", sizeof(x));      // 4 on most systems
printf("%d", sizeof(char));   // 1
printf("%d", sizeof(float));  // 4

It is useful for knowing the size of types on a particular machine and for allocating memory correctly. (The sizes shown may differ on some systems.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 Second Term · Science Morning · 4 marks

Explain about relational operators in C.

Answer

Relational operators compare two values (operands) and give a result that is either true (1) or false (0). They are mostly used in conditions of if, while and for.

OperatorMeaningExample (a = 10, b = 5)Result
>Greater thana > b1 (true)
<Less thana < b0 (false)
>=Greater than or equal toa >= 101 (true)
<=Less than or equal tob <= 40 (false)
==Equal toa == b0 (false)
!=Not equal toa != b1 (true)

Example:

int a = 10, b = 5;
printf("%d", a > b);   // prints 1
printf("%d", a == b);  // prints 0

Note: == compares two values, whereas = is the assignment operator. Mixing them up is a common mistake.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 Second Term · Management Morning · 4 marks

What is a data type in the C language? Explain about any two types.

Answer

Data type: A data type defines the kind of value a variable can store, the amount of memory it occupies and the operations that can be performed on it. For example, int, float and char.

1. int (integer type): It stores whole numbers without a fractional part, positive or negative. It normally occupies 4 bytes (2 bytes on older compilers) and uses the format specifier %d.

int age = 17;

2. float (floating-point type): It stores real numbers with a fractional part, with about 6 decimal digits of precision. It occupies 4 bytes and uses the format specifier %f.

float height = 5.8;

Other basic types are char (a single character, 1 byte, %c) and double (larger-precision real numbers, 8 bytes, %lf).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Model Set D · 1+4 marks

What is an array? Explain its types.

Answer

Array: An array is a collection of elements of the same data type stored in contiguous memory locations and referred to by a single name. Each element is accessed by its index (subscript), which starts at 0.

Example: int marks[5]; creates five integer elements marks[0] to marks[4].

Types of array:

1. One-dimensional (1-D) array: a linear list of elements with a single subscript.

int a[5] = {10, 20, 30, 40, 50};

2. Two-dimensional (2-D) array: elements arranged in rows and columns like a table or matrix, with two subscripts: array[row][column].

int m[2][3] = {{1, 2, 3}, {4, 5, 6}};

Here m[1][2] is 6. A 2-D array is used for matrices.

3. Multi-dimensional array: arrays with more than two subscripts, such as a three-dimensional array int x[2][3][4];, which holds 2×3×4=242 \times 3 \times 4 = 24 elements.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Annual · Trinity (Science) · 8 marks
  • Trinity Class 11 Computer Science Third Term · 8 marks

Write a program in C language to input any ten numbers. Now sort them in ascending order and print them.

Answer

Logic: Read the ten numbers into an array, then sort them using the bubble sort method. In each pass, adjacent elements are compared and swapped if the left one is larger. After the passes, the array is in ascending order.

#include <stdio.h>

int main()
{
    int a[10], i, j, temp;

    printf("Enter ten numbers:\n");
    for (i = 0; i < 10; i++)
    {
        scanf("%d", &a[i]);
    }

    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9 - i; j++)
        {
            if (a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }

    printf("Numbers in ascending order:\n");
    for (i = 0; i < 10; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}

Sample run: input 5 2 9 1 7 3 8 6 4 0 gives output 0 1 2 3 4 5 6 7 8 9.

Explanation: The first loop reads the numbers. The nested loops perform the bubble sort: the outer loop controls the number of passes (9) and the inner loop compares neighbours, swapping them with the help of temp. The last loop prints the sorted array.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Model Set B · 8 marks
  • Trinity Class 11 Computer Science Third Term · 8 marks

Write a program in C language, using switch, to display the name of the day on the basis of an entered number. E.g. if you enter 1, it says "It is Sunday".

Answer

#include <stdio.h>

int main()
{
    int day;

    printf("Enter a number (1-7): ");
    scanf("%d", &day);

    switch (day)
    {
        case 1: printf("It is Sunday");    break;
        case 2: printf("It is Monday");    break;
        case 3: printf("It is Tuesday");   break;
        case 4: printf("It is Wednesday"); break;
        case 5: printf("It is Thursday");  break;
        case 6: printf("It is Friday");    break;
        case 7: printf("It is Saturday");  break;
        default: printf("Invalid number. Enter 1 to 7.");
    }
    return 0;
}

Sample run: input 1 gives It is Sunday; input 9 gives Invalid number. Enter 1 to 7.

Explanation: The number is read into day. The switch compares it with each case; the matching case prints the day name and break stops the program from running into the next case. The default block handles any number outside 1 to 7.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Model Set C · 8 marks

Write a program to show the concept of any four string handling functions.

Answer

The string handling functions are declared in the header file string.h. The program below demonstrates strlen(), strcpy(), strcat() and strcmp().

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

int main()
{
    char s1[30] = "Nepal";
    char s2[30] = "Kathmandu";
    char s3[30];

    printf("Length of s1 = %d\n", strlen(s1));

    strcpy(s3, s1);
    printf("After strcpy, s3 = %s\n", s3);

    strcat(s1, s2);
    printf("After strcat, s1 = %s\n", s1);

    if (strcmp(s2, s3) == 0)
        printf("s2 and s3 are equal\n");
    else
        printf("s2 and s3 are not equal\n");

    return 0;
}

Output:

Length of s1 = 5
After strcpy, s3 = Nepal
After strcat, s1 = NepalKathmandu
s2 and s3 are not equal

Functions used:

  • strlen(s) returns the number of characters in the string (without the null character).
  • strcpy(dest, src) copies the source string into the destination.
  • strcat(s1, s2) appends s2 to the end of s1.
  • strcmp(s1, s2) returns 0 if the strings are equal, a negative value if s1 is smaller and a positive value if s1 is greater.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Third Term · Science Day · Trinity · 8 marks

Write a program to demonstrate the use of any seven string manipulation functions.

Answer

The following program, using the header file string.h, demonstrates seven functions: strlen(), strcpy(), strncpy(), strcat(), strcmp(), strchr() and strstr().

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

int main()
{
    char a[40] = "Education";
    char b[40] = "Nepal";
    char c[40], d[40];

    /* 1. strlen */
    printf("1. strlen(a) = %d\n", (int)strlen(a));

    /* 2. strcpy */
    strcpy(c, a);
    printf("2. strcpy: c = %s\n", c);

    /* 3. strncpy */
    strncpy(d, a, 3);
    d[3] = '\0';
    printf("3. strncpy (3 chars): d = %s\n", d);

    /* 4. strcat */
    strcat(c, b);
    printf("4. strcat: c = %s\n", c);

    /* 5. strcmp */
    printf("5. strcmp(a, b) = %s\n",
           strcmp(a, b) == 0 ? "equal" : "not equal");

    /* 6. strchr */
    printf("6. strchr(a, 'c') -> %s\n", strchr(a, 'c'));

    /* 7. strstr */
    printf("7. strstr(c, \"Nep\") -> %s\n", strstr(c, "Nep"));

    return 0;
}

Output:

1. strlen(a) = 9
2. strcpy: c = Education
3. strncpy (3 chars): d = Edu
4. strcat: c = EducationNepal
5. strcmp(a, b) = not equal
6. strchr(a, 'c') -> cation
7. strstr(c, "Nep") -> Nepal

Functions used:

  1. strlen(s): length of string.
  2. strcpy(dest, src): copies a whole string.
  3. strncpy(dest, src, n): copies at most n characters (the null character is added manually).
  4. strcat(s1, s2): joins s2 to the end of s1.
  5. strcmp(s1, s2): compares two strings; 0 means equal.
  6. strchr(s, ch): returns a pointer to the first occurrence of a character, so the rest of the string from there is printed.
  7. strstr(s1, s2): returns a pointer to the first occurrence of substring s2 in s1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Model Set D · 8 marks

An array contains 20 elements. Write a program to find the total number of positive and negative numbers.

Answer

Logic: Read the 20 elements into an array and examine each one. If an element is greater than 0, increase the positive counter; if it is less than 0, increase the negative counter. Zero is neither positive nor negative.

#include <stdio.h>

int main()
{
    int a[20], i;
    int positive = 0, negative = 0;

    printf("Enter 20 numbers:\n");
    for (i = 0; i < 20; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] > 0)
            positive++;
        else if (a[i] < 0)
            negative++;
    }

    printf("Total positive numbers = %d\n", positive);
    printf("Total negative numbers = %d\n", negative);
    return 0;
}

Sample run: if the 20 numbers contain 12 numbers above zero, 7 below zero and one zero, the output is Total positive numbers = 12 and Total negative numbers = 7.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Third Term · Science Morning · Trinity · 8 marks

Write a program to read two 2×22 \times 2 matrices and display their sum using array.

Answer

Logic: Add the corresponding elements of the two matrices: Cij=Aij+BijC_{ij} = A_{ij} + B_{ij}. Nested loops are used for each row and column of the 2×22 \times 2 arrays.

#include <stdio.h>

int main()
{
    int a[2][2], b[2][2], sum[2][2];
    int i, j;

    printf("Enter elements of first matrix:\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &a[i][j]);

    printf("Enter elements of second matrix:\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &b[i][j]);

    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            sum[i][j] = a[i][j] + b[i][j];

    printf("Sum of the matrices:\n");
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
            printf("%d\t", sum[i][j]);
        printf("\n");
    }
    return 0;
}

Sample run: for A=[1234]A=\begin{bmatrix}1&2\\3&4\end{bmatrix} and B=[5678]B=\begin{bmatrix}5&6\\7&8\end{bmatrix}, the result is A+B=[681012]A+B=\begin{bmatrix}6&8\\10&12\end{bmatrix}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Second Term · Science Day · Trinity · 5 marks

Write a program to check if the given number is prime or composite.

Answer

Logic: A prime number has exactly two factors, 1 and itself. A composite number has more than two factors. Count the divisors from 2 up to n/2n/2; if any divides nn exactly, the number is composite. (Numbers below 2 are neither prime nor composite.)

#include <stdio.h>

int main()
{
    int n, i, flag = 0;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n < 2)
    {
        printf("%d is neither prime nor composite", n);
        return 0;
    }

    for (i = 2; i <= n / 2; i++)
    {
        if (n % i == 0)
        {
            flag = 1;
            break;
        }
    }

    if (flag == 0)
        printf("%d is a prime number", n);
    else
        printf("%d is a composite number", n);

    return 0;
}

Sample run: for 13 no number from 2 to 6 divides it, so it is prime. For 12, 12 mod 2=012 \bmod 2 = 0, so it is composite.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 Second Term · Science Day · Trinity · 3+3+2 marks

Write a menu-based program, using switch, to perform the following tasks. (a) Calculate the sum of all elements of an array (b) Multiplication table of any given number (c) Exit If we enter 1, it performs sum; if we enter 2, it performs the multiplication table; if we enter 3 it terminates.

Answer

One switch handles the three menu options: (a) sum of array elements, (b) multiplication table and (c) exit.

#include <stdio.h>

int main()
{
    int choice, i, n, num;
    int a[100], sum;

    printf("1. Sum of array elements\n");
    printf("2. Multiplication table\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice)
    {
        case 1:
            printf("How many elements? ");
            scanf("%d", &n);
            sum = 0;
            printf("Enter the elements:\n");
            for (i = 0; i < n; i++)
            {
                scanf("%d", &a[i]);
                sum = sum + a[i];
            }
            printf("Sum = %d", sum);
            break;

        case 2:
            printf("Enter a number: ");
            scanf("%d", &num);
            for (i = 1; i <= 10; i++)
                printf("%d x %d = %d\n", num, i, num * i);
            break;

        case 3:
            printf("Program terminated");
            break;

        default:
            printf("Invalid choice");
    }
    return 0;
}

Sample run: choice 2 with number 5 prints 5 x 1 = 5 up to 5 x 10 = 50. Choice 1 with elements 4 6 10 prints Sum = 20. Choice 3 ends the program (alternatively, exit(0) from stdlib.h may be used).

Explanation: case 1 accumulates the elements into sum; case 2 uses a for loop from 1 to 10 to print the table; case 3 terminates the program; default handles any other entry.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 Second Term · Science Morning · 4 marks

Write a program to input marks in five subjects and find the total with percentage.

Answer

Logic: Read the marks of five subjects, add them to get the total, and calculate

Percentage=Total marks500×100\text{Percentage} = \frac{\text{Total marks}}{500} \times 100

assuming each subject is out of 100.

#include <stdio.h>

int main()
{
    float m1, m2, m3, m4, m5, total, percentage;

    printf("Enter marks in five subjects: ");
    scanf("%f %f %f %f %f", &m1, &m2, &m3, &m4, &m5);

    total = m1 + m2 + m3 + m4 + m5;
    percentage = total / 500 * 100;

    printf("Total = %.2f\n", total);
    printf("Percentage = %.2f%%", percentage);
    return 0;
}

Sample run: for marks 80 70 90 60 75, the total is 80+70+90+60+75=37580+70+90+60+75=375 and the percentage is 375500×100=75%\frac{375}{500}\times 100 = 75\%.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 Second Term · Management Morning · 5 marks

Write a program in C language to find the circumference of a circle (c=2πrc = 2\pi r with π=3.14\pi = 3.14, as given in the paper: c=23.14r).

Answer

The circumference of a circle is c=2πrc = 2\pi r, where π=3.14\pi = 3.14 and rr is the radius.

#include <stdio.h>
#include <conio.h>

void main()
{
    float r, c;
    clrscr();
    printf("Enter the radius of the circle: ");
    scanf("%f", &r);
    c = 2 * 3.14 * r;
    printf("Circumference = %.2f\n", c);
    getch();
}

For example, if r=7r = 7, then c=2×3.14×7=43.96c = 2 \times 3.14 \times 7 = 43.96.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 Second Term · Management Morning · 4 marks

Write a program using C language to find the area and perimeter of a square (area =l2= l^2, perimeter =4l= 4l).

Answer

For a square of side ll, area =l2= l^2 and perimeter =4l= 4l.

#include <stdio.h>
#include <conio.h>

void main()
{
    float l, area, perimeter;
    clrscr();
    printf("Enter the side of the square: ");
    scanf("%f", &l);
    area = l * l;
    perimeter = 4 * l;
    printf("Area = %.2f\n", area);
    printf("Perimeter = %.2f\n", perimeter);
    getch();
}

For example, if l=6l = 6, the output is Area = 36.00 and Perimeter = 24.00.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 Second Term · Management Day · 4 marks

Write a C program to find the square root of (a+b)(a + b).

Answer

The library function sqrt() is declared in math.h, so that header must be included.

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
    float a, b, sum, root;
    clrscr();
    printf("Enter two numbers a and b: ");
    scanf("%f%f", &a, &b);
    sum = a + b;
    root = sqrt(sum);
    printf("Square root of (a + b) = %.2f\n", root);
    getch();
}

For example, if a=7a = 7 and b=9b = 9, then a+b=16a + b = 16 and the output is 16=4.00\sqrt{16} = 4.00.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 Second Term · Management Day · 5 marks

Write a program to input your name and section. Then print them.

Answer

A string (character array) is used to store the name and the section. gets() is used for the name so that spaces in it are accepted.

#include <stdio.h>
#include <conio.h>

void main()
{
    char name[30], section[5];
    clrscr();
    printf("Enter your name: ");
    gets(name);
    printf("Enter your section: ");
    gets(section);
    printf("Name: %s\n", name);
    printf("Section: %s\n", section);
    getch();
}

For example, if the input is Ram Sharma and A, the output is Name: Ram Sharma and Section: A.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Trinity Class 11 Computer Science Third Term · 1 mark

Which of the following is NOT a jumping statement? (a) break (b) continue (c) goto (d) for

Answer

(d) for

break, continue and goto transfer control to another point in the program, so they are jumping statements. for is a looping statement.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 1 MCQ

Loading…

  • Trinity Class 11 Computer Science Third Term · 5 marks

Explain the switch case statement of the C language.

Answer

The switch statement is a multi-way decision (selection) statement. It compares the value of one expression with a list of constant values (cases) and executes the block of the matching case. It is a cleaner alternative to a long if...else if ladder when one variable is compared with many fixed values.

Syntax:

switch (expression)
{
    case value1:
        statement(s);
        break;
    case value2:
        statement(s);
        break;
    ...
    default:
        statement(s);
}

Working:

  1. The expression is evaluated once. It must give an integer or character value.
  2. Its value is compared with each case constant in order.
  3. When a match is found, the statements of that case are executed.
  4. break ends the switch. Without break, execution continues into the following cases (fall-through).
  5. If no case matches, the default block (optional) is executed.

Rules: case values must be unique constants (not variables or ranges), and float values cannot be used.

Example:

#include <stdio.h>
int main()
{
    int n = 2;
    switch (n)
    {
        case 1: printf("One"); break;
        case 2: printf("Two"); break;
        default: printf("Other");
    }
    return 0;
}

Output: Two.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

Questions from CDC Class 11 Computer Science Model Question (New Course) (CDC/NEB model paper, Sub.Code 4271, first Class 11 new-course batch), NEB Grade XI Computer Science 2076 (2019) Sub. Code 130 'B' (old course) (NEB board exam, old course, 75 marks; only questions that fit the current outline were taken), Trinity International SS & College Class 11 Computer Science 4271 Annual 2078 (Science) (School annual exam (not NEB), 2078 (2021), 50 marks, as posted on plkcomputersir.blogspot.com), Trinity International SS & College Class 11 Computer Science 4271 Annual 2078 (Management) (School annual exam (not NEB), 2078 (2021), 50 marks), Class 11 Computer Science Five Set Model Questions 2078 (Sets A-E) (Model question sets 2078 following the CDC grid (Set A reprints the CDC model); compiled on plkcomputersir.blogspot.com), Trinity Class 11 Computer First Class Test 2078 (Sets A and B) (School class test, 25 marks, 2078), Trinity Class 11 Computer Second Class Test 2078 (Morning and Day) (School class test, 25 marks, 2078), Trinity Class 11 Computer Science First Term 2078 (School first-term exam, 2078 (2021), 50 marks), Trinity Class 11 Computer Science Second Term 2078 (Science, Morning and Day) (School second-term exam, 2078, 50 marks), Trinity Class 11 Computer Science Second Term 2078 (Management, Morning and Day) (School second-term exam, 2078, 50 marks), Class 11 Computer Science Second Term 2079 (Science, Morning and Day) (School second-term exam, 2079, 50 marks (Day paper is only partly posted)), Class 11 Computer Science Second Term 2079 (Management, Morning and Day) (School second-term exam, 2079, 50 marks), Trinity Class 11 Computer Science Third Term 2078 (Science, Morning and Day) (School third-term exam, 2078, 50 marks) and Trinity Class 11 Computer Science Third Term (Management, Morning and Day) (School third-term exam, page title 2078 but the paper header reads 3rd Term Examination 2079, 50 marks). Answers are written for this site; check them against your class notes.

Chapter titles and hours from the official syllabus ↗