Important topic from 014 ~Software development and analysis

Few important topics

011~ C and problem solving

1. Fibonacci series using recursion
2. Flow chart of a loop program
3. String lower n upper case
4. Matrices calculations
5. Stored class in c
6. While, do while, for loop
7. Switch and If-else ladder


Architecture MCQ focuses on “Basic Operational Concept of The Processor”.

1. The decoded instruction is stored in ______ .
a) IR
b) PC
c) Registers
d) MDR
View Answer

Answer:a
Explanation: The instruction after obtained from the PC, is decoded and operands are fetched and stored in the IR.

2. The instruction -> Add LOCA,R0 does,
a) Adds the value of LOCA to R0 and stores in the temp register
b) Adds the value of R0 to the address of LOCA
c) Adds the values of both LOCA and R0 and stores it in R0
d) Adds the value of LOCA with a value in accumulator and stores it in R0
View Answer

Answer:c
Explanation: None.

3. Which registers can interact with the secondary storage ?
a) MAR
b) PC
c) IR
d) R0
View Answer

Answer:a
Explanation: MAR can interact with secondary storage in order to fetch data from it.

4. During the execution of a program which gets initialized first ?
a) MDR
b) IR
c) PC
d) MAR
View Answer

Answer:c
Explanation: For the execution of a process first the instruction is placed in the PC.

5. Which of the register/s of the processor is/are connected to Memory Bus ?
a) PC
b) MAR
c) IR
d) Both a and b
View Answer

Answer:b
Explanation: MAR is connected to the memory BUS in order to access the memory

6. ISP stands for,
a) Instruction Set Processor
b) Information Standard Processing
c) Interchange Standard Protocol
d) Interrupt Service Procedure
View Answer

Answer:a
Explanation: None.

7. The internal Components of the processor are connected by _______ .
a) Processor intra-connectivity circuitry
b) Processor bus
c) Memory bus
d) Rambus
View Answer

Answer:b
Explanation: The processor BUS is used to connect the various parts in order to provide a direct connection to the CPU.

8. ______ is used to choose between incrementing the PC or performing ALU operations .
a) Conditional codes
b) Multiplexer
c) Control unit
d) None of these
View Answer

Answer:b
Explanation: The multiplexer circuit is used to choose between the two as it can give different results based on the input.

9. The registers,ALU and the interconnection between them are collectively called as _____ .
a) Process route
b) Information trail
c) information path
d) data path
View Answer

Answer:d
Explanation: The Operational and processing part of the CPU are collectively called as data path.

10. _______ is used to store data in registers .
a) D flip flop
b) JK flip flop
c) RS flip flop
d) none of these
View Answer

Answer:a
Explanation: None.

viva & exam questions for C

2.  What does static variable mean?There are 3 main uses for the static.
1. If you declare within a function: It retains the value between function calls
2. If it is declared for a function name: By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files
3. Static for global variables: By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file.
#include <stdio.h>
int t = 10;
main(){
int x = 0;
void funct1();
funct1();         
printf("After first call \n");
funct1();         
printf("After second call \n");
funct1();         
printf("After third call \n");
}
void funct1()
{
    static int y = 0;
    int z = 10;          
    printf("value of y %d z %d",y,z);
    y=y+10;
}
value of y 0 z 10 After first call
value of y 10 z 10 After second call
value of y 20 z 10 After third call
3.  What are the different storage classes in C?C has three types of storage: automatic, static and allocated.  Variable having block scope and without static specifier have automatic storage duration.
Variables with block scope, and with static specifier have static scope. Global variables (i.e, file scope) with or without the the static specifier also have static scope.  Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class.



8.  What is a null pointer?There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro NULL, defined in , has a value that’s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*.
Some people, notably C++ programmers, prefer to use 0 rather than NULL.
The null pointer is used in three ways:
1) To stop indirection in a recursive data structure.
2) As an error value.
3) As a sentinel value.
9.  What is the output of printf("%d") ?When we write printf("%d",x); this means compiler will print the value of x. But as here, there is nothing after %d so compiler will show in output window garbage value.
10.  What is the difference between calloc() and malloc() ?calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size).
malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...).
malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to set the new handler mode.
11.  What is the difference between printf() and sprintf() ?sprintf() writes data to the character array whereas printf(...) writes data to the standard output device.





15.  What is the difference between strings and character arrays?A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicity specified by using the static keyword.
Actually, a string is a character array with following properties:
* the multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character.
* it not specified what happens if this array, i.e., string, is modified.
* Two strings of same value[1] may share same memory area.


18.  Which bit wise operator is suitable for turning off a particular bit in a number?The bitwise AND operator, again. In the following code snippet, the bit number 24 is reset to zero.
some_int = some_int & ~KBit24;
19.  Which bit wise operator is suitable for putting on a particular bit in a number?The bitwise OR operator. In the following code snippet, the bit number 24 is turned ON:
some_int = some_int | KBit24;
20. Does there exist any other function which can be used to convert an integer or a float to a string?Some implementations provide a nonstandard function called itoa(), which converts an integer to string.
#include
char *itoa(int value, char *string, int radix);
DESCRIPTION
The itoa() function constructs a string representation of an integer.
PARAMETERS
value: Is the integer to be converted to string representation.
string: Points to the buffer that is to hold resulting string.
The resulting string may be as long as seventeen bytes.
radix: Is the base of the number; must be in the range 2 - 36.
A portable solution exists. One can use sprintf():
char s[SOME_CONST];
int i = 10;
float f = 10.20;
sprintf ( s, “%d %f\n”, i, f );

22.  Difference between const char* p and char const* pIn const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location.
In char const* p, the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’.


29.  When does the compiler not implicitly generate the address of the first element of an array?Whenever an array name appears in an expression such as
·  array as an operand of the size of operator
·  array as an operand of & operator
·  array as a string literal initializer for a character array
Then the compiler does not implicitly generate the address of the address of the first element of an array.
30.  Why n++ executes faster than n+1 ?The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.
31.  Why doesn't the following statement work?char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
Answer:  The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below:
strcat ( str, "!" ) ;


33.  What is the purpose of main( ) function ?The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution.
· It is the starting function
· It returns an int value to the environment that called the program
· Recursive call is allowed for main( ) also.
· It is a user-defined function
· Program execution ends when the closing brace of the function main( ) is reached.
· It has two arguments 1)argument count and 2) argument vector (represents strings passed).
· Any user-defined name can also be used as parameters for main( ) instead of argc and argv
34.  How can I search for data in a linked list?Unfortunately, the only way to search a linked list is with a linear search, because the only way a linked list’s members can be accessed is sequentially.
Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.
35.  Why should we assign NULL to the elements (pointer) after freeing them?This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to dangle; it doesn’t point at anything useful.
If you NULL out or zero out a pointer immediately after freeing it, your program can no longer get in trouble by using that pointer. True, you might go indirect on the null pointer instead, but that’s something your debugger might be able to help you with immediately.
Also, there still might be copies of the pointer that refer to the memory that has been deallocated; that’s the nature of C. Zeroing out pointers after freeing them won’t solve all problems.



85. What is dangling pointer in c?If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
86. What are merits and demerits of array in c?Merits:
(a) We can easily access each element of array.
(b) Not necessity to declare too many variables.
(c) Array elements are stored in continuous memory location.
Demerits:
(a) Wastage of memory space. We cannot change size of array at the run time.
(b) It can store only similar type of data
87. Where are the auto variables stored?Auto variables are stored in main memory and their default value is a garbage value.
88. Why Preincrement operator is faster than Postincrement?Evaluation of any expression is from left to right. Preincrement is faster because it doesn't need to save the current value for next instruction whereas Postincrement needs to saves current value to be incremented after execution of current instruction.
89. Difference between arrays and linked list?Major differences between arrays and linked lists are: (i)  In array consecutive elements are stored in consecutive memory locations whereas in linked list it not so. (ii)  In array address of next element is consecutive and whereas in linked list it is specified in the address part of each node.(iii) Linked List makes better use of memory than arrays.(iv) Insertion or deletion of an element in array is difficult than insertion or deletion in linked list
90. What is the use of typedef?(i)It increases the portability.
(ii) It simplify the complex declaration and improve readability of the program.
91. What are library Functions?Library Functions are predefined functions and stored in .lib files.
92. What is a structure?Structure is a collection of heterogeneous (i.e. related data items which can be of different types) held together to a single unit. The data items enclosed within a structure are called its members which may be of data type int, float, char, array etc.
93. What is a pointer?Pointer is a variable that contains address of another variable in the memory. Pointers are quite useful in creation of linked data structures (such as linked lst, trees graphs), managing object allocated memory dynamically, optimize the program to execute faster and use less memory.
94. What are the techniques you use for debugging?(i)Using compiler’s features
(ii)Read The Fine Module
(iii)printf( ) debugging
(iv)Code grinding
(v)Assertion

96. What is difference between Structure and Unions?(i)    In structure every member has its own memory whereas in union its members share the same member space.
(ii)  In structure, it is possible to initialize all the members at the same time which is not possible in case of union.
(iii) A structure requires more space than union(for the same type of members).
(iv) In union different interpretations of the same memory space are possible which is not so in case of structures.
97. What are the advantages of using Unions?(i) Efficient use of memory as it it does not demand memory space for its all members rather it require memory space for its largest member only.
(ii) Same memory space can be interpreted differently for different members of the union.
98. What is the difference between ordinary variable and pointer in C?An ordinary variable is like a container it can hold any value and we can change the value of ordinary variable at a time throughout the program .A pointer is a variable that stores the address of another Variable.
99. What are segment and offset addresses?When paging technique is performed, the page will breaks into segments and its sequence is said to be segments and its width can be said as offset. In short,segment is a physical address and offset is logical address.
100. When should a type cast be used?There are two situations in which to use a type cast. The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly.
The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers. For example, the following line type casts the return value of the call to malloc() to be a pointer to a foo structure.
struct foo *p = (struct foo *) malloc(sizeof(struct foo));
101. What is the difference between %d and %*d in c language?%d give the original value of the variable and %*d give the address of the variable.
eg:-int a=10,b=20;
printf("%d%d",a,b);
printf("%*d%*d",a,b);
Result is 10 20 1775 1775 .Here 1775 is the starting address of the memory allocation for the integer.a and b having same address because of contagious memory allocation.
102. How does a C program come to know about command line arguments? When we execute our C program, operating system loads the program into memory. In case of DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file tables,environment segment, and command line information. When we compile the C program the compiler inserts additional code that parses the command, assigning it to the argv array, making the arguments easily accessible within our C program.
103. How are pointer variables initialized? Pointer variable are initialized by one of the following two ways
- Static memory allocation
- Dynamic memory allocation
104. What is modular programming?If a program is large, it is subdivided into a number of smaller
programs that are called modules or subprograms. If a complex
problem is solved using more modules, this approach is known as
modular programming
105. Where does global, static, local, register variables and C Program instructions get stored?Global , static, local :  In main memory       
Register variable: In registers
C program : In main memory.
106. Where are the auto variables stored?Auto variables are stored in main memory and their default value is a garbage value.
107. What is an lvalue?An lvalue is an expression to which a value can be assigned. The lvalue
expression is located on the left side of an assignment statement,
whereas an rvalue is located on the right side of an assignment
statement. Each assignment statement must have an lvalue and an
rvalue. The lvalue expression must reference a storable variable in
memory. It cannot be a constant
108. What is an argument? Differentiate between formal arguments and actual arguments?An argument is an entity used to pass the data from calling function to
the called function. Formal arguments are the arguments available in
the function definition. They are preceded by their own data types.
Actual arguments are available in the function call.
109. When is a switch statement better than multiple if statements?A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.
110. Differentiate between a linker and linkage?A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the
linkage of variable.
111. Define Operator, Operand, and Expression in 'C'?Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.
Operands are variables or expressions which are used in operators to evaluate the expression.
Combination of operands and operators form an expression.


129. what is nested structure?A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name for convenient handling. Structures can contain other structures as members; in other words, structures can nest.
130. What is slack byte in structure?To store any type of data in structure there is minimum fixed byte which must be reserved by memory. This minimum byte is known as word boundary. Word boundary depends upon machine. TURBO C is based on 8086 microprocessor which has two byte word boundary. So any data type reserves at least two byte space.
131.What is prototype of printf function? Prototype of printf function is:
 int printf( const char *format ,…)
132.What is difference between declaration and definition? During declaration we just specify the type and no memory is allocated to the variable. But during the
 definition an initial value is assigned and memory is allocated to the variable.
133. What is function recursion?When a function of body calls the same function then it is called as 'recursive function.'
Example:
Recursion()
{
    printf("Recursion !");
    Recursion();
}


144. Describe turbo c compiler?Turbo c is an IDE of c programming language created by Borland. Turbo C 3.0  is based on MS DOS operation  system. It is one of the most popular c compilers. It uses 8086 microprocessor which is 16 bit microprocessor. It has 20 address buses and 16 data bus. Its word length is two byte.
145. Out of fgets() and gets() which function is safe to use and why?fgets() is safer than gets(), because we can specify a maximum input length. Neither one is completely safe, because the compiler can’t prove that programmer won’t overflow the buffer he pass to fgets ().
146. Difference between strdup and strcpy?Both copy a string. strcpy wants a buffer to copy into. strdup allocates a buffer using malloc().
Unlike strcpy(), strdup() is not specified by ANSI .
147. Differentiate between a for loop and a while loop? What are it uses?For executing a set of statements fixed number of times we use for loop while when the number of
iterations to be performed is not known in advance we use while loop.
148. What is storage class? What are the different storage classes in C?Storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. The storage classes in c are auto, register, and extern, static, typedef.
149. What are the uses of a pointer?(i)It is used to access array elements
(ii)It is used for dynamic memory allocation.
(iii)It is used in Call by reference
(iv)It is used in data structures like trees, graph, linked list etc.
150.In header files whether functions are declared or defined?Functions are declared within header file. That is function prototypes exist in a header file,not function bodies. They are defined in library (lib).
151. Difference between pass by reference and pass by value?Pass by reference passes a pointer to the value. This allows the callee to modify the variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks a reference to it.)
152. What are enumerations?They are a list of named integer-valued constants. Example:enum color { black , orange=4,yellow, green, blue, violet };This declaration defines the symbols “black”, “orange”, “yellow”, etc. to have the values “1,” “4,” “5,” … etc. The difference between an enumeration and a macro is that the enum actually declares a type, and therefore can be type checked.
153. Are pointers integer?No, pointers are not integers. A pointer is an address. It is a positive number.
154. What is static memory allocation?Compiler allocates memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation.
155. What is dynamic memory allocation?A dynamic memory allocation uses functions such as malloc() or calloc() to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these function are assigned to pointer variables, such a way of allocating memory at run time is known as dynamic memory allocation.
156. What modular programming?If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming
157. What is a function?A large program is subdivided into a number of smaller programs or subprograms. Each subprogram specifies one or more actions to be performed for the larger program. Such sub programs are called functions.
158. Difference between formal argument and actual argument?Formal arguments are the arguments available in the function definition. They are preceded by their own data type. Actual arguments are available in the function call. These arguments are given as constants or variables or expressions to pass the values to the function.
159. what are C tokens?There are six classes of tokens: identifier, keywords, constants, string literals, operators and other separators.
160. What are C identifiers?These are names given to various programming element such as variables, function, arrays.It is a combination of letter, digit and underscore.It should begin with letter. Backspace is not allowed.
161. Difference between syntax vs logical error?Syntax Error
These involves validation of syntax of language.
compiler prints diagnostic message.
Logical Error
logical error are caused by an incorrect algorithm or by a statement mistyped in such a way that it doesn’t violet syntax of language.
difficult to find.
162. What are the facilities provided by preprocessor?file inclusion
substitution facility
conditional compilation
163.What do the functions atoi(), itoa() and gcvt() do?atoi() is a macro that converts integer to character.
itoa() It converts an integer to string
gcvt() It converts a floating point number to string
164. What is FILE?FILE is a predefined data type. It is defined in stdio.h file.
165. What is a file?A file is a region of storage in hard disks or in auxiliary storage devices.It contains bytes of information .It is not a data type.
Important topics



Assignment based viva for Descret mathematics MCS013 held on Sunday.

Lab incharge

Descrete Mathematics - 22 Nov 2015
timing between Lab session. 8am to 2pm

Viva MCS013, MCSL016, MCSL017

All will be on Sunday between lab session
bhari se bhari sankhya me aakar Ignou ko dhanya karen.

cheers
Akshay

1 (a). Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart.

Ans.

/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i,fact=1;
 printf("Enter any number : ");
 scanf("%d"&n);
 for(i=1; i<=n; i++)
    fact = fact * i;
 printf("Factorial value of %d = %d",n,fact);
 return 0;
}

The output of above program would be:
Output of calculate factorial value  of a number C program
Screen shot for calculate factorial value
of a number C program



Algorithm for calculate factorial value of a number:

[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
        i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]

Flowchart for calculate factorial value of a number:

flowchart for calculate factorial value of a number
Figure: Flowchart for calculate factorial value of
a number C program




1 (b) Using recursion, generate 'n' terms of 10 Fibonacci series (n > 0).


C code to print Fibonacci series without recursion:

#include<stdio.h>

void printFibonacci(int);

int main(){

    int k,n;
    long int i=0,j=1,f;

    printf("Enter the range of the Fibonacci series: ");
    scanf("%d %d ",&n);

    printf("Fibonacci Series: ");
    printf("%d ",0);
    printFibonacci(n);

    return 0;
}

void printFibonacci(int n){

    long int first=0,second=1,sum;

    while(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         n--;
    }
}



1 (c) Using file handling, create a file, insert some 10
characters and count them.

Ans.
#include<stdio.h>
#include<conio.h>
 
void main()
{
 char ch;
 int count=0;
 FILE *fptr;
 clrscr();
 fptr=fopen("text.txt","w");
 if(fptr==NULL)
 {
  printf("File can't be created\a");
  getch();
  exit(0);
 }
 printf("Enter some text and press enter key:\n");
 while((ch=getche())!='\r')
 {
  fputc(ch,fptr);
 }
 fclose(fptr);
 fptr=fopen("text.txt","r");
 printf("\nContents of the File is:");
 while((ch=fgetc(fptr))!=EOF)
 {
  count++;
  printf("%c",ch);
 }
 fclose(fptr);
 printf("\nThe number of characters present in file is: %d",count);
 getch();
}




1 (d). Using pointers concept, reverse a given 10
string.




Ans.
Logic: The approach here is to reverse the string using the pointers. Reversing the string includes the reversing the each and every words in it. After accepting a string from user, it calls a function “strev” with two string pointer arguments, the source and destination. It has an iterative loop, which traces from the EOL through the beginning. Each time it copies the current letter to the destination. Finally it displays the resultant string.
The earlier program implements the same by direct method, i.e. without pointers.

#include<stdio.h>
#include<conio.h>
void strev(char *str1, char *str2);
void main()
{
char *str1, *str2;
clrscr();
printf("\n\n\t ENTER A STRING...: ");
gets(str1);
strev(str1,str2);
printf("\n\t THE REVERSED STRING IS...: ");
puts(str2);
getch();
}

void strev(char *str1, char *str2)
{
int i = 0, len = 0, r = 0;
while(*(str1+len)!='\0')
len++;
for(i=len-1; i>=0; i--)
{
*(str2+r) = *(str1+i);
r++;
}
*(str2+r) = '\0';
}



 


2 (a). Write a program to find the string length 10
without using strlen() function 

#include<conio.h>
#include<stdio.h>
void main(){
char *str;
int count = 0,i;
clrscr();
printf("\nEnter the String: ");
gets(str);
for(i=0;str[i]!='\0';i++){
count++;
}
printf("\nThe length of the string is %d.",count);
getch();
}