Free Programming Basics MCQs with Answers

183 Programming Basics MCQs from Computer Science and IT, each with the correct answer and a written explanation of why it is correct. Free and unlimited, with no account needed.

Programming basics covers algorithms, flowcharts, pseudocode, variables, constants, data types, operators, input and output, conditional statements and loops. It also introduces functions, arrays, strings, debugging and common programming errors, with attention to the difference between syntax errors, runtime errors and logical errors in a program.

Last updated

183 questions · page 9 of 10

  • A. Different type expression
  • B. Same type expression
  • C. int. float type expression
  • D. Mixed type expression

Explanation: The correct option is (d) Mixed type expression. An expression is a combination of values, variables, and operators that evaluates to a single value. When an expression contains operands of different data types, it is called a mixed type expression. In order for the expression to be valid, the programming language needs to perform type casting, which is the conversion of a value from one data type to another.

Correct answer: Mixed type expression
  • A. Exponentiation
  • B. Multiplication
  • C. Integer remainder
  • D. Division

Explanation: The modulus operator, denoted by %, is used to find the remainder after a division operation. For example, 13 % 5 equals 3, because 5 goes into 13 two times with a remainder of 3.

Correct answer: Integer remainder
  • A. Expressions
  • B. Comments
  • C. Operator procedures
  • D. Equal operator

Explanation: Comments are lines of code that are ignored by the compiler. They are used to provide information about the code for programmers who are reading it. Comments can be single-line comments, which start with // and continue to the end of the line, or multi-line comments, which start with /* and end with */.Expressions are combinations of variables, operators, and function calls that evaluate to a value.Operator procedures are not a standard feature of the C language. However, some C compilers support operator overloading, which allows programmers to define custom operators that work with specific data types.Equal operator is a binary operator that is used to compare two values.

Correct answer: Comments
  • A. 1
  • B. 3
  • C. 8
  • D. 5

Explanation: The correct option is (b) 3. In most programming languages, the percent (%) operator is the modulo operator, which calculates the remainder after a division operation. So, 10 % 3 is 1 because 10 divided by 3 leaves a remainder of 1.

Correct answer: 3
  • A. Modulus
  • B. Increment
  • C. Decrement
  • D. All of above

Explanation: The increment operator, denoted by ++, is used to increase the value of a variable by 1. For example, if x = 5, then ++x will make x equal to 6

Correct answer: Increment
  • A. Numbers
  • B. Underscore (_)
  • C. Letters
  • D. Period (.)

Explanation: While some languages allow periods within variable names (like Python), it's generally discouraged because periods have a special meaning in many languages. For example, they are often used to separate parts of a complex name, like customer.address.

Correct answer: Period (.)
  • A. j = 21
  • B. j = 20
  • C. j = 2
  • D. i = 23

Explanation: The correct option is (c) j = 2.Let's break down the expression to understand how we arrive at the answer:i = 0: This assigns the initial value of 0 to the variable i.(i++): This part uses the post-increment operator. The value of i (which is 0 here) is used in the expression first, and then i is incremented by 1. So, in this case, the value used is 0.(++i): This part uses the pre-increment operator. Here, i is incremented by 1 first (which becomes 1 from the previous step), and then the incremented value (1) is used in the expression.Now, let's evaluate the expression step by step:(i++) + (++i) becomes (0) + (1).Since the post-increment operator uses the value before incrementing, i remains 0 in the first part.The pre-increment operator increments i to 1 before using its value in the second part.Therefore, j = 0 + 1.So, the final value of j is 2.

Correct answer: j = 2
  • A. Zero
  • B. One
  • C. Less than Zero
  • D. Both (a) and (b)

Explanation: While both 0 and 1 are used in some languages to represent boolean values, a relational expression typically evaluates to just one of these values (0 for false).

Correct answer: Both (a) and (b)
  • A. (*)
  • B. //
  • C. \
  • D. :

Explanation: In many programming languages, // is the syntax for a single-line comment. Any code following // on the same line will be ignored by the compiler.

Correct answer: //
  • A. a123
  • B. My name
  • C. int
  • D. Both (b) and (c)

Explanation: The correct option is (c) int. Variable names follow specific rules depending on the programming language. Here's why the other options are valid and why (c) is not:Reason why (c) int is not valid:The keyword int is typically reserved in programming languages. Keywords are predefined words that have special meanings within the language. They are used for control flow, data types, functions, and other core functionalities. Using a keyword as a variable name would cause a conflict with the language's syntax and would not be allowed.

Correct answer: int
  • A. Function
  • B. Data types
  • C. Keywords
  • D. Both (b) and (c)

Explanation: Function: Functions are blocks of code that perform a specific task. While functions are essential components of a program, they are not directly used by the compiler as an aid in compilation. The compiler processes functions like any other code block, translating them into machine code.Constant macro: Constant macros are preprocessor directives that define constants within the code. While macros can be used to improve code readability and maintainability, they are not essential for compilation. The compiler can process code without them.Constant macro: Constant macros are preprocessor directives that define constants within the code. While macros can be used to improve code readability and maintainability, they are not essential for compilation. The compiler can process code without them.

Correct answer: Both (b) and (c)
  • A. Addition (+)
  • B. Subtraction (-)
  • C. Multiplication (*)
  • D. Division (/)

Explanation: In C, addition, subtraction, and multiplication are binary operators, meaning they operate on two operands. They perform the usual mathematical operations of adding, subtracting, and multiplying two numbers, respectively.Division, however, can be different depending on the data types of the operands.Integer division: When dividing integers, the result is also an integer, and any remainder is discarded. For example, 13 / 5 equals 2 (with a remainder of 3 that is discarded).Floating-point division: When dividing floating-point numbers, the result is a floating-point number representing the exact quotient. For example, 13.0 / 5.0 equals 2.6.This distinction between integer and floating-point division makes division slightly different from the other arithmetic operators in C.

Correct answer: Division (/)
  • A. a=77
  • B. b=40
  • C. a=117
  • D. Compile time error

Explanation: int a,b;: This line declares two integer variables a and b.a=77;: This line assigns the value 77 to variable a.b=40;: This line assigns the value 40 to variable b.a+=b;: This line is an assignment expression. The += operator is a shorthand for a = a + b. So, in this line, the value of b (which is 40) is added to the current value of a (which is 77), and the result (117) is stored back into variable a.printf("a= %d", a);: This line prints the value of variable a to the console. Since a now holds the value 117, the output will be:a= 117

Correct answer: a=117
  • A. 6
  • B. 7
  • C. 8
  • D. run time error

Explanation: The correct option is (b) 7.Let's break down the code step by step:a=117: This line assigns the value 117 to the variable a. However, this line has no effect on the rest of the code because a is not used anywhere else in the provided code snippet.int number =6;: This line declares an integer variable named number and initializes it with the value 6.++number;: This line uses the pre-increment operator. The value of number (which is 6 here) is incremented by 1 first, and then the incremented value (7) is used in the rest of the code.printf("%d\n", number);: This line prints the value of number using the printf function. The %d format specifier is used to print an integer value, and \n adds a newline character after printing.Since number was incremented to 7 in the previous step, this line prints 7 followed by a newline.

Correct answer: 7
  • A. 15
  • B. 16
  • C. 17
  • D. Compile time error

Explanation: The code contains two errors that would lead to a compilation error:Data type mismatch: In C, integer is not a valid data type. The correct data types for integers are int, short, long, or long long.Missing semicolon: There is a missing semicolon after the printf statement.Here's the corrected code:Cint x = 155;printf("%d", x);.This code would output:155

Correct answer: Compile time error
  • A. 10
  • B. 15
  • C. 10 16
  • D. 11 16

Explanation: The correct option is (d) 11 16.Let's break down the code and analyze the errors:int x=10°: The degree symbol (°) is not a valid numeric literal in C. The code should use int x = 10; to assign the value 10 to the variable x.Missing semicolons: The lines \[y=15\] and \[x=x++:\] are missing semicolons at the end. In C, semicolons are required to terminate statements.Incorrect increment syntax: The syntax x=x++: is not a valid way to use the pre-increment operator in C. The correct way is ++x.Invalid printf format specifier: In the printf statement, %d%d is used to print two integers. However, there is no dot (.) separator between x and y. To print separate values, you need to use a comma (,) to separate them.Here's a corrected version of the code:Cint x = 10;int y = 15;x = ++x; // Pre-increment x to 11y = ++y; // Pre-increment y to 16printf("%d %d", x, y);.With these corrections, the code will output:11 16Explanation of the corrected code:int x = 10; int y = 15;: These lines declare and initialize the variables x and y.x = ++x;: This line uses the pre-increment operator to increment x by 1 before assigning its value. So, x becomes 11.y = ++y;: Similar to the previous line, y is incremented to 16 before assigning the value.printf("%d %d", x, y);: This line prints the values of x (which is 11) and y (which is 16) with a space between them.Now the code compiles and runs without errors, producing the expected output of 11 16.

Correct answer: 11 16
  • A. 10
  • B. -5
  • C. -6
  • D. 6

Explanation: The code performs the following steps:Variable Initialization:Declares an integer variable number and assigns it the value 6.Declares another integer variable x and assigns it the value 0.Negation:The line x = - number; assigns the negative of number to the variable x. In this case, the negative of 6 is -6, so x becomes -6.Print:The line printf("%d ", x); prints the value of x which is -6.

Correct answer: -6
  • A. (x=25)
  • B. (x=100)
  • C. (x=75)
  • D. Compile time error

Explanation: The correct option is (c) (x=75).Let's break down the code step by step and explain why other options are incorrect:int x, y;: This line declares two integer variables x and y.x=100:: This line assigns the value 100 to the variable x. The colon (:) at the end is a syntax error. In C, a semicolon (;) is used to terminate statements.y=25;: This line correctly assigns the value 25 to the variable y.x-=y;: This line subtracts the value of y (which is 25) from x and stores the result back in x. So, x becomes 100 - 25 = 75.printf(" x = %d", x);: This line prints the value of x using the printf function. The %d format specifier is used to print an integer value.Since x was assigned the value 75 in the previous step, this line prints "x = 75".

Correct answer: (x=75)
  • A. 20
  • B. 40
  • C. 60
  • D. 80

Explanation: The code defines three integer variables: x, y, and Z.Variable Initialization:x is assigned the value 20.y is assigned the value 40.Calculation:Z is assigned the sum of x and y. Since x is 20 and y is 40, Z becomes 20 + 40 = 60.Print:The printf statement prints the value of Z, which is 60.

Correct answer: 60
  • A. a=3
  • B. a=5
  • C. a=15
  • D. a=18

Explanation: Let's break down the code step by step:#include <stdio.h>: This line includes the standard input/output library, which provides functions like printf for printing to the console.main: This is the main function where the program execution begins.int a=5;: This line declares an integer variable a and initializes it with the value 5.int b=3;: This line declares another integer variable b and initializes it with the value 3.a *= b;: This line is an assignment expression. The *= operator is a shorthand for a = a * b. So, in this line, the value of b (which is 3) is multiplied by the current value of a (which is 5), and the result (15) is stored back into variable a.printf("a=%d", a);: This line prints the value of variable a to the console. Since a now holds the value 15, the output will be:a= 15

Correct answer: a=15