Monday 28 September 2020

Name different categories of constants in C language.

 Question: Name different categories of constants in C language.

Answer

A constant is an identifier whose value cannot be changed throughout the execution of a program whereas the variable value keeps on changing. In C there are four basic types of constants. They are:

1. Integer constants
2. Floating point constants
3. Character constants
4. String constants

Example

Integer constant: 147
Floating point constants: 34.56
Character constants: 'A', '\r'
String constants: "A", "EFH"

Define the term 'variable'. What are the rules to be followed to name a variable in "C"? Write the syntax to declare a variable and also mention how to assign values to it (initialize them).

 Question:

Define the term 'variable'. What are the rules to be followed to name a variable in "C"? Write the syntax to declare a variable and also mention how to assign values to it (initialize them).

Answer

Variable is an identifier whose value changes from time to time during execution. It is a named data storage location in your computer’s memory. By using a variable’s name in your program, you are, in effect, referring to the data stored there.

A variable represents a single data item i.e. a numeric quantity or a character constant or a string constant.

A value must be assigned to the variables at some point of time in the program which is termed as assignment statement.

The variable can then be accessed later in the program. If the variable is accessed before it is assigned a value, it may give garbage value.

All variables have three essential attributes:

  • the name
  • the value
  • the memory, where the value is stored.

variable

Declaring Variables

int a;
int num1, num2;
float amount;

Initialising Variables

The value is assigned at the declaration time. For example,

int b = 10;

The value can be assigned anywhere inside the program using = operator after it is declared.

num1 = 89;

Rules for Forming Identifiers

Identifiers are defined according to the following rules:

1. It consists of letters and digits.
2. First character must be an alphabet or underscore.
3. Both upper and lower cases are allowed. Same text of different case is not equivalent, for example: TEXT is not same as text.
4. Except the special character underscore ( _ ), no other special symbols can be used. For example,
some valid identifiers are shown below:
X
X123
_XI
temp
tax_rate

What are the different types of errors in C? Explain with examples.

 Question: What are the different types of errors in C? Explain with examples.

Answer

Errors are the problems or the faults that occur in the program. There are commonly five types of errors exist in C programming:

  1. Syntax error
  2. Run-time error
  3. Linker error
  4. Logical error
  5. Semantic error

Syntax error

Syntax errors are also known as the compilation errors as they occurred at the compilation time. These errors are mainly occurred due to the mistakes while typing when programmer does not follow the grammar rule of the programming language. These errors can be easily corrected.

Commonly occurred syntax errors are: using variable without its declaration, missing the semicolon (;) at the end of the statement.

syntax-error

Run-time error / Execution Error

The errors exist during the execution-time even after the successful compilation known as run-time errors. When the program is running, and it is not able to perform the operation is the main cause of the run-time error. The division by zero is the common example of the run-time error. These errors are very difficult to find, as the compiler does not point out to these errors.

run-time-error

Linker error

Linker errors are mainly generated when the executable file of the program is not created. This can be happened either due to the wrong function prototyping or usage of the wrong header file.

linker-error

Logical error

The logical error is an error that leads to an undesired output. These errors produce the incorrect output, but they are error-free, known as logical errors. The occurrence of these errors mainly depends upon the logical thinking of the developer.

logical-error

Semantic error

Semantic errors are the errors that occurred when the statements are not understandable by the compiler. The following can be the cases for the semantic error:

semantic-error

Give the syntax and examples of usage of scanf() and printf().

 Question: Give the syntax and examples of usage of scanf() and printf().

Answer

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

printf("format string",argument_list); 

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function

The scanf() function is used for input. It reads the input data from the console.

scanf("format string",argument_list);

Example of input and output in C language that prints addition of Two numbers.

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

void main()
{    
    int x, y, sum;  
  
    printf("enter first number:");  
    scanf("%d", &x);  
    printf("enter second number:");  
    scanf("%d", &y);  
  
    sum=x+y;  
    printf("Sum of two numbers: %d ", sum);  
    getch();
}

Output

enter first number:9
enter second number:4
Sum of two numbers: 13

Give some distinctive features of 'C' which states it to be a structured programming. What are the differences between a low level, a middle level and a high-level language?

 Question 1

Give some distinctive features of 'C' which states it to be a structured programming. What are the differences between a low level, a middle level and a high-level language?

Answer:

C is called a structured programming language because you can solve a large problem by dividing the problem into smaller structural blocks (function) each of which handles a particular responsibility. Even a bigger structural block like a function can have smaller inner structural blocks like decisions and loops.

Structured programming came into picture after the assembly language. Structural support was not there in the assembly languages. Programming languages used to use goto and jump type of statements to hop between different logical units. A bigger program used to be unorganized and difficult to understand.

Some Structure features in C

  • sequence (statements are executed in order one after another)
  • selection (if, switch-case)
  • iteration (for, while, do-while)
  • subroutines (function)
  • and blocks ({})

Low level languages / Machine Oriented Languages

The language whose design is governed by the circuitry and the structure of the machine is known as the Machine language. This language is difficult to learn and use. It is specific to a given computer and is different for different computers i.e. these languages are machine-dependent. These languages have been designed to give a better machine efficiency, i.e. faster program execution. Such languages are also known as Low Level Languages.

High level Languages

They are easy to learn and programs may be written in these languages with much less effort. However, the computer cannot understand them and they need to be translated into machine language with the help of other programs known as Compilers or Translators. Like C++, Java, Python

Middle level Languages

It bridges gap between machine understandable machine level language and more conventional high-level language. This programming helps in writing system programming as well as application programming. Because C language has both the feature of high level and low-level language so it is often called middle level language.

Saturday 26 September 2020

 

Multiple Choice Questions Set A

There are four alternate answers of each of the following questions. One or more of them is/are correct. Tick (Ö) the best answer.

1)                  Pascal is a unit of

a)      power

b)      pressure

c)      force

d)      velocity

2)                  Joule is a unit of

a)      work

b)      energy

c)      both (a) and (b)

d)      none

3)                  Unit of weight is

a)      kilogram

b)      kilometer

c)      kilowatt

d)      Newton

4)                  Unit of power is

a)      Watt

b)      kilowatt

c)      Horse power

d)      All of the above

5)                  SI unit of temperature is

a)      Degree Farhenheight

b)      Degree Celcius

c)      Kelvin

d)      Rumer

6)                  The property of a body, due to which a body remains or tends to remain in it’s initial state of rest or uniform motion in straight line, is called

a)      Inertia

b)      inertia of rest

c)      inertia of motion

d)      All of the above

7)                  A body is moving with a velocity of 25 m/s. Unless external and imbalanced force acts upon it, it’s velocity does not change. It is Newton’s

a)      1st law of motion

b)      2nd law of motion

c)      3rd law of motion

d)      All of the above

8)                  A force of 60 N is applied to a body of 15 kg, which produces an acceleration (in SI unit)

a)      900

b)      90

c)      4

d)      40

9)                  A body of mass 2 kg is thrown vertically upward with a velocity of 25 m/s (from the earth surface). When does the body returns to the thrower’s hand?

a)      2.5 s

b)      5 s

c)      10 s

d)      15 s

10)              A force applied on a body can change it’s

a)      shape

b)      state of rest

c)      state of uniform motion

d)      All of the above

11)              ‘A’ starts from rest and gains a velocity of 40 m/s in 5 seconds. It’s acceleration is

a)      0 m/s2

b)      4 m/s2

c)      8 m/s2

d)      10 m/s2

12)              A body, on the earth surface, is thrown vertically upward. After certain interval of time it reaches maximum height. where

a)      it’s velocity is zero

b)      it’s acceleration is 10 m/s2

c)      both (a) and (b)

d)      none of the above

13)              A body, moving in a circular path, may have

a)      constant speed

b)      constant velocity

c)      constant acceleration

d)      none of the above

14)              When a bullet is fired, from a gun, the gun recoils. It explains Newton’s

a)      1st law of motion

b)      2nd law of motion

c)      3rd law of motion

d)      gravitational law

15)              A vehicle, whose velocity and acceleration are 20 m/s and -5 m/s2 respectively, comes in rest after

a)      1 s

b)      2 s

c)      3 s

d)      4 s

16)              A body, moving with uniform velocity, has acceleration equal to

a)      9.8 m/s2

b)      10 m/s2

c)      0 m/s2

d)      None of the above

17)              Velocity ratio in pulley system is given by

a)     

b)     

c)      both of the above

d)      none  of the above

18)              Velocity ratio of a lever is always less than one. Which type of lever is that?

a)      1st class

b)      2nd class

c)      3rd class

d)      all of the above

19)              Which one is not affected by friction of a machine?

a)      effort distance

b)      load distance

c)      velocity ratio

d)      all of the above

20)              Efficiency of a machine is always less than 100 % due to

a)      MA is affected by friction

b)      VR is affected by friction

c)      both of the above

d)      none of the above

MCQ Question Bank Microsoft PowerPoint