0% found this document useful (0 votes)
27 views2 pages

C Programming Exam Questions and Tasks

Uploaded by

pokebl4z3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

C Programming Exam Questions and Tasks

Uploaded by

pokebl4z3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

FACULTY OF ENGINEERING

DEPARTMENT OF COMPUTER ENGINEERING


2022/2023 FIRST SEMESTER [Link] EXAMINATION
COURSE CODE: ECE 2105
COURSE TITLE: BASIC COMPUTER PROGRAMMING
SEMESTER: FIRST SESSION: 2022/2023
DATE: DURATION: 2HRS

INSTRUCTION: Answer Question ONE and ANY other THREE Questions


1(a) Determine which of the following are valid identifiers. If invalid, explain why.
(i) Record1 (ii) 1record (iii) file_3 (iv) return (v) $tax (5 marks)
(b) Mention 5 standard keywords in C programming that you know (5 marks)
(c) Write a C programming to add two numbers together (9 marks)
(d) Describe the general form (syntax) of the following statements
(i) The FOR statement (ii) The WHILE statement (iii) The SWITCH statement
(6 marks)

2(a) Describe the following with examples


(i) Expressions (ii) Statement (6 marks)
(b) Write a complete C program to determine the roots of the quadratic equation
2 −b ± √ b2−4 ac
a x +bx +c=0 using the well-known quadratic formula. x= . Allow for
2a
the possibility that one of the constants has a value of zero, and that the quantity
2
b −4 ac is less than or equal to zero. (9 marks)

3(a) A C program contains the following declarations and initial assignments


int i=8, j=5, k;
float x=0.005, y=-0.01, z;
char a, b, c =’c’, d=’d’; - Ascii code of ‘c’ is 99 and ‘d’ is 100
Determine the value of each of the following assignment expressions. Use the values
originally assigned to the variables for each expression
(i) k=(j==5)? i:j (ii) k=(j>5)?i:j (iii) z=(x>=0)?x:0 (iv) i-=(j>0)? j:0 (8 marks)
(b) What are unary operators? How many operands are associated with a unary operator?
(3 marks)
(c) Mention TWO most commonly used unary operators and describe them with
examples.
(4 marks)

4(a) What are the commonly used input/output functions in C? How are they accessed?
(5 marks)
(b) A C program contains the following statements:
#include <stdio.h>
Char a, b, c;
(i) Write appropriate getchar statements that will allow values for a, b and c to be
entered into the computer. (3 marks)
(ii) Write appropriate putchar statements that will allow the current values of a, b
and c to be written out of the computer (that is, to be displayed). (3 marks)
(iii) Solve (i) using a single scanf function and (ii) using a single printf function
rather than the getchar and putchar statements. Compare your answer with
solution to (i) and (ii). (4 marks)

5(a) Write a C program to find the average score of n quiz done by you this semester
(11 marks)
(b) What is the meaning of the following conversion characters for data output
(i) c (ii) d (iii) e (iv) f (v) s (4 marks)

6(a) Write a C program to input principle, time and rate (P, T, R) from user and find
Simple Interest SI=P*T*R/100. (4
marks)
(b) Which of the following are valid string constants
(i) ‘a’ (ii) ‘\\’ (iii) ‘\0’ (iv) ‘$90 (v) ‘\052’ (3 marks)
(c) Name and describe the four basic data types in C. (8 marks)

Common questions

Powered by AI

Conversion characters in C are used in 'printf' functions to specify the type of data to be printed. The character 'c' prints a single character. 'd' is for decimal integers, 'e' is for scientific notation of floats, 'f' for floating-point numbers in normal notation, and 's' prints string of characters. Each conversion character adjusts how the data is formatted, ensuring values are displayed properly based on their type. For example, 'printf("%d", 10);' outputs '10', while 'printf("%f", 10.5);' outputs '10.500000' .

Evaluating assignment expressions in C involves following operator precedence rules and using initially declared variable values. For example, with 'int i=8, j=5, k;', evaluating 'k=(j==5)?i:j' results in k=8 as the condition is true, so 'i' is assigned. For 'k=(j>5)?i:j', k=5 since the condition is false, assigning 'j'. In 'z=(x>=0)?x:0' with float x=0.005, 'x' is positive, so z=0.005. With 'i-=(j>0)? j:0', because j>0, 'i' becomes i-j=3. These evaluations show how expressions are assessed according to precedence and conditions .

Unary operators in C programming operate on a single operand and perform operations such as incrementing/decrementing a value or determining its type. Common unary operators include the increment ('++') and decrement ('--') operators. They are unique because they change or utilize only one operand, whereas binary or ternary operators require two or more operands. For example, using '++i' or 'i++' increases the value of 'i' by one .

In C programming, managing input and output can be done effectively using functions like 'scanf' and 'printf' for formatted user input and output. However, for character-by-character manipulation, 'getchar()' and 'putchar()' are available, providing finer control. For example, to read and display characters 'a', 'b', 'c', 'getchar()' and 'putchar()' are used: 'a = getchar();', 'putchar(a);'. Alternatively, 'scanf("%c%c%c", &a, &b, &c); printf("%c%c%c", a, b, c);' handles it in a single input/output operation, allowing batch processing and easier management compared to individual character functions .

Some common standard keywords in C programming include 'int', 'return', 'if', 'for', and 'while'. These keywords serve specific roles and instructions within the programming language, such as declaring variables, controlling the flow of execution, and creating loops or conditional statements .

In C programming, the 'FOR' statement is used for loops and has the syntax: 'for(initialization; condition; increment) { //code block }'. The 'WHILE' statement continuously executes a block of code as long as a specified condition is true, with the syntax: 'while(condition) { //code block }'. The 'SWITCH' statement allows the execution of code among various options based on a variable's value: 'switch(expression) { case value1: //code block; break; ... default: //code block; }' .

To calculate simple interest in C, you need the principal amount 'P', time period 'T', and rate 'R'. The formula used is Simple Interest (SI) = P*T*R/100. A simple C program for this is: 'int main() { float P, T, R, SI; scanf("%f %f %f", &P, &T, &R); SI = (P*T*R)/100; printf("Simple Interest is: %.2f", SI); return 0; }'. This program takes user input for principal, time, and rate, and computes the simple interest using the defined formula .

In C programming, an identifier is considered valid if it begins with a letter (either uppercase or lowercase) or an underscore, followed by any combination of letters, digits, or underscores. For example, 'Record1' is valid because it starts with a letter and includes letters and digits. '1record' is invalid because it begins with a digit. 'file_3' is valid as it starts with a letter and follows the rules. 'return' is invalid as it's a reserved keyword in C. Lastly, '$tax' is invalid because the dollar sign is not permitted in identifiers .

In C, to calculate the roots of a quadratic equation given by ax^2 + bx + c = 0, using the formula x = (−b ± √(b^2−4ac))/(2a), one must first check whether a, b, or c are zero to avoid division by zero and handle cases where b^2−4ac ≤ 0 for complex roots. For example: 'int main() { float a, b, c, discriminant, root1, root2; scanf("%f %f %f", &a, &b, &c); discriminant = b*b - 4*a*c; if(discriminant >= 0 && a != 0) { root1 = (-b + sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) / (2*a); printf("Roots are: %.2f, %.2f", root1, root2); } else { printf("Complex roots or invalid input"); } }' .

The four basic data types in C are 'int', 'char', 'float', and 'double'. 'int' is used for integers and is significant for numerical operations. 'char' holds single characters and is integral to handling text. 'float' and 'double' are for floating-point numbers, with 'double' providing more precision. Each data type is essential for different programming needs, enabling efficient and appropriate data storage .

You might also like