• No results found

TYPICAL QUESTIONS & ANSWERS

N/A
N/A
Protected

Academic year: 2021

Share "TYPICAL QUESTIONS & ANSWERS"

Copied!
158
0
0

Loading.... (view fulltext now)

Full text

(1)

TYPICAL QUESTIONS & ANSWERS

PART - I,

OBJECTIVE TYPE QUESTIONS

Each Question carries 2 marks.

Choose correct or the best alternative in the following: Q.1 Literal means

(A) a string. (B) a string constant. (C) a character. (D) an alphabet. Ans:B

Q.2 Choose the correct answer

(A) Casting refers to implicit type conversion. (B) Coercion refers to implicit type conversion. (C) Casting means coercion.

(D) Coercion refers to explicit type conversion.

Ans:B

Q.3 printf (“%d”, printf (“tim”));

(A) results in a syntax error (B) outputs tim3

(C) outputs garbage (D) outputs tim and terminates abruptly Ans:B

printf statement will print tim3, “tim” due to inner printf statement and 3 as length due to outer printf statement.

Q.4 Output of the following program fragment is

x = 5; y = x++; printf(“%d%d”, x, y); (A) 5, 6 (B) 5, 5 (C) 6, 5 (D) 6, 6 Ans:C

x is incremented by 1 and before that increment the value is assigned to y so value of x=6 and y=5.

Q.5 The value of an automatic variable that is declared but not initialised will be

(A) 0 (B) -1

(C) unpredictable (D) none of these Ans:C

(2)

Q.6 Consider the following program main ( ) { float a = 0.5, b = 0.7; if (b < 0.8) if (a < 0.5) printf (“ABCD”); else printf (“PQR”); else printf (“JKLF); } The output is (A) ABCD (B) PQR (C) JKLF (D) None of these Ans:B

Since b=0.7<0.8, the control goes to second “if” statement where (a<0.5) is false to printf statement in else part executed printing “PQR”

Q.7 The following program fragment

int *a; *a = 7;

(A) assigns 7 to a (B) results in compilation error (C) assigns address of a as 7 (D) segmentation fault

Ans:D

Q.8 A pointer variable can be

(A) passed to a function as argument. (B) changed within function.

(C) returned by a function. (D) assigned an integer value. Ans:C

Q.9 ‘C’ is often called a

(A) Object oriented language (B) High level language (C) Assembly language (D) Machine level language Ans:B

Q.10 The loop in which the statements within the loop are executed at least once is called (A) do-while (B) while

(C) for (D) goto

Ans:A

Q.11 The control automatically passes to the first statement after the loop in (A) continue statement (B) break statement

(3)

Ans:B

Q.12 A self contained block of statements that perform a coherent task of some kind is called a

(A) Monitor (B) Function

(C) Program (D) Structure

Ans:B

Q.13 Recursion is sometimes called

(A) Circular definition (B) Complex definition (C) Procedure (D) Union

Ans:A

Q.14 Unsigned integer occupies

(A) Two bytes (B) Four bytes

(C) One byte (D) Eight bytes

Ans:B

Q.15 Each C preprocessor directive begins with

(A) # (B) include (C) main() (D) { Ans:A Q.16 main() { long i = 30000; printf(“%d”, i); } the output is (A) 3000 (B) 30000 (C) 0 (D) -1 Ans:B

Q.17 The directive that can be used to test whether an expression evaluates to a nonzero value or not

is

(A) #if (B) #elif

(C) #endif (D) #exit

Ans:A Q.18 main() {

printf(“%p\n”, main()); }

(4)

(B) Prints 0. (C) Is an error.

(D) Is an infinite loop. Ans:A

Q.19 The << operator is used for

(A) Right shifting (B) Left shifting

(C) Bitwise shifting (D) Bitwise complement Ans:B

Q.20 The C language includes the header file standard input & output in (A) stdlib.h library (B) stdio.h library

(C) conio.h library (D) #include library

Ans:B

Q.21 The value that follows the keyword CASE may only be

(A) constants (B) variable

(C) number (D) semicolon

Ans:A

Q.22 The statement which is used to terminate the control from the loop is

(A) break (B) continue

(C) goto (D) exit

Ans:A

Q.23 The machine registers are sometimes called

(A) local variables (B) global variables (C) accumulators (D) static variables Ans:A

Q.24 Set of values of the same type, which have a single name followed by an index is called

(A) function (B) structure

(C) array (D) union

Ans:C

Q.25 An array of pointers is same as

(A) pointer to array (B) pointers to pointers (C) pointer to function (D) pointer to structure

(5)

Ans:B

Q.26 What is the output of the following program segment?

main() { long i = 65536; printf(“%d\n”, i); } (A) 0 (B) 65536 (C) -1 (D) 65 Ans:A

Q.27 What is the output of the following program segment?

main() { int i = 1; do { printf(“%d..”, i); } while(i--); } (A)0..1.. (B) 1..0.. (C) 0 (D) -1 Ans:B

Q.28 What is the output of the following program segment?

main() { int i = ++2; printf(“%d\n”, i); } (A) 3 (B) 2 (C) 0 (D) -1 Ans:A

It is a compilation error. However if we write “i=2; ++i;” then value of i is printed as 3. Q.29 The name of all functions end with a

(A) pair of parenthesis (B) semicolon

(C) braces (D) colon

Ans:A

Q.30 A float variable can store any variable within the range of

(6)

(C) −7.2×1038 to 7.2×1038 (D) −1.2×1038 to 1.2×1038

Ans:B

Q.31 scanf() can be used for reading

(A) double character (B) single character (C) multiple characters (D) no character Ans:C

Q.32 ‘C’ allows a three-way transfer of control with the help of (A) unary operator (B) relational operator (C) ternary operator (D) comparison operator Ans:C

Q.33 The statement that transfers control to the beginning of the loop is called (A) break statement (B) exit statement

(C) continue statement (D) goto statement Ans:C

Q.34 A variable which is visible only in the function in which it is defined, is called (A) static variable (B) auto variable

(C) external variable (D) local variable Ans:D

Q.35 The number of arguments supplied from the command line, by convention, is known as

(A) arg c (B) arg v

(C) #define (D) #include

Ans:A

Q.36 Output of the program given below is

int i; main() { printf(“%d”, i); } (A) 1 (B) 0 (C) -1 (D) Null Ans:B

(7)

main() { char *p = “ayqm”; printf (“%c”, ++*(p++));} (A) b (B) z (C) q (D) n Ans:A

Q.38 What will be the output of the following program?

main() { int i = 5; printf(“%d”, i=++i==6); } (A) 0 (B) 1 (C) 7 (D) 6 Ans:B

Q.39 Determine which of the following is a valid character constant

(A) ‘\\’ (B) ‘\0’

(C) ‘xyz’ (D) ‘\052’

Ans:A

Q.40 The maximum value that an integer constant can have is

(A) .32767 (B) 32767

(C) 1.7014e+38 (D) –1.7014e+38 Ans:B

Q.41 The expression X=4+2%-8 evaluates

(A) –6 (B) 6

(C) 4 (D) None

Ans:B

Q.42 What will be the output of following program?

main() { int x=15; printf(“\n%d%d%d”, x!=15, x=20, x<30); } (A)0, 20, 1 (B) 15, 20, 30 (C) 0, 0, 0 (D) Error

(8)

Ans:A

Q.43 How many times the following program would print (“abc”)?

main() {

printf(“\nabc”);

main();

}

(A) Infinite number of times (B) 32767 times

(C) 65535 times (D) Till the stack does not overflow Ans:A

Q.44 What would be output of the following program?

# define SQR(X) (X*X) main() { int a, b=3; a = SQR(b+2); printf(“\n%d”, a); } (A) 25 (B) 11

(C) Error (D) Garbage value

Ans:B

Q.45 What would be output of the following program?

#include "stdio.h" main()

{

printf(“%d%d”, size of (NULL!), size of (“ “)); }

(A) 2 1 (B) 1 2

(C) 2 2 (D) 1 1

Ans:C

Q.46 What would be output of the following program, if the array begins at 65486?

main() {

int arr[ ] = {12, 14, 15, 23, 45}; printf(“%u%u”, arr+1, &arr+1); }

(A) 65486, 65486 (B) 65488, 65488

(9)

Ans:C

Array begins at address 65486 so arr+1=65488 as size of int is 2 bytes and &arr+1=65486+10=65496 as there are 5 elements in array.

Q.47 Given the statement, maruti.engine.bolts=25; which of the following is true?

(A) Structure bolts is nested within structure engine (B) Structure engine is nested within structure maruti (C) Structure maruti is nested within structure engine (D) Structure maruti nested within structure bolts Ans:B

Q.48 Which amongst the following is not a keyword?

(A) external (B) int

(C) float (D) double

Ans:A

Q.49 If a = 5 and b = 7 then the statement p = (a > b) : a ? b

(A) assigns a value 5 to p (B) assigns a value 7 to p (C) assigns a value 8 to p (D) gives an error message Ans:D

Q.50 The expression P >> 6 shifts all bits of P six places to right. What is the value of P >> 6 if 7 db 6 0 P= × ? (A) 0×1234 (B) 0×0001 (C) 0×0000 (D) 0×1B6 Ans:D

Q.51 If an integer occupies 4 bytes and a character occupies 1 bytes of memory, each element of the following structure would occupy how many bytes?

struct name { int age; char name [20]; } (A) 5 (B) 24 (C) 21 (D) 22 Ans:B

Q.52 If an array is used as function argument, the array is passed

(10)

(C) by name. (D) the array cannot be used as a function argument. Ans:B

Q.53 To access a structure element using a pointer, ___________ operator is used

(A) dot (.) (B) pointer (&)

(C) pointer (*) (D) arrow (→)

Ans:D

Q.54 The library function sqrt( ) operates on a double precision argument. If, i is an integer variable, which one of the following calls would correctly compute sqrt(i)?

(A) sqrt((double)i) (B) (double) sqrt(i) (C) (double) (sqrt(i)) (D) sqrt(i)

Ans:A

Q.55 What will happen if the following loop is executed?

int num = 0; do { --num; printf(“%d”, num); num++; }while (num >= 0); }

(A) The loop will run infinite number of times. (B) The program will not enter the loop. (C) There will be a compilation error. (D) There will be runtime error. Ans:C

Q.56 The break statement causes an exit (A) Only from the innermost loop. (B) Only from the innermost switch. (C) From the innermost loop or switch. (D) From the program.

Ans:C

Q.57 It is necessary to declare the type of function in the calling program if (A) Function returns an integer.

(B) Function returns a non-integer value. (C) Function is not defined in the same file. (D) Function is called number of times.

(11)

Ans:B

Q.58 The function fprintf is used in a program

(A) When too many printf calls have been already used in the program. (B) In place of printf, since printf uses more memory.

(C) When the output is to be printed on to a file.

(D) When the type of variables to be printed are not known before. Ans:C

Q.59 The following statement displays

float x = 2000.53; printf (“%e”, x);

(A) 2.00053e+04 (B) 2.00053e+03

(C) 2.00053+e04 (D) 2.0005e+03

Ans:B

Q.60 The output of the following is

int a = 75;

printf (“%d%%”, a);

(A) 75 (B) 75%%

(C) 75% (D) None of the above

Ans:D

Q.61 C language was invented by

(A) Abacus (B) Charles babage

(C) Thomson (D) Dennis Ritchie

Ans:D

Q.62 The given FOR loop is

for ( ; ; )

{

printf(“ ”);

}

(A) valid (B) indefinite

(C) invalid (D) displays runtime errors Ans:D

The given for loop displays runtime errors because no test condition is given. Test condition is must inside for loop.

Q.63 The following code displays

(12)

{ int *p; p = (int*) malloc(sizeof(int)); *p = 10; printf(“p = %d\n”, *p); } (A) 10 (B) 1542 (address of p)

(C) 20 (D) None of the above

Ans:A

Q.64 The _______ operator is a technique to forcefully convert one data type to the others

(A) Cast (B) Conversion

(C) Type (D) Uniary

Ans:A

Q.65 The output of the following will be

for (x=1, y=5; x+y<=10; x++)

{ printf(“%d%d”, x,y); y++; } (A) 1 5 (B) 1 5 2 6 2 6 3 7 3 7 4 8 (C) 1 5 (D) 1 5 1 6 2 5 1 7 3 5 1 8 4 5 1 9 5 5 Ans:A

Q.66 The __________ statement causes immediate exit from the loop overriding the condition test

(A) Exit (B) Break

(C) Goto (D) None of the above

Ans:B

Q.67 The output of the following code is

a = 5; a << 1;

(13)

(A) 5 (B) 6

(C) 2 (D) 3

Ans:A

Q.68 The purpose for mode “w+b” in file operation is (A) create a binary file for write

(B) create a binary file for read/write (C) open a binary file for writing

(D) open a binary file for reading/writing Ans:B

Q.69 The operators << and >> are

(A) assignment operator (B) relational operator (C) logical operator (D) bitwise shift operator Ans D

Q.70 Which of the following numerical value is invalid constant

(A) .75 (B) 9.3e2

(C) 27,512 (D) 123456

Ans C

Q.71 A C program contains the following declaration int i=8, j=5 what would be the value of following expression? abs(i-2*j) (A) 2 (B) 4 (C) 6 (D) 8 Ans A

Q.72 What will be the output of the following program

main( ) { int k, num=30; k=(num>5 ? (num<= 10 ? 100:200):500); printf(“\n%d”, num); } (A) 100 (B) 5 (C) 30 (D) 500 Ans C

Q.73 What is the output of the following code int n = 0, m=1;

(14)

do { printf(“%d”, m); m++; } while(m<=n); (A) 0 (B) 2 (C) 1 (D) 4 Ans C

Q.74 If a=8 and b=15 then the statement

x= (a>b) ? a:b;

(A) assigns a value 8 to x (B) gives an error message (C) assigns a value 15 to x (D) assigns a value 7 to x Ans C

Q.75 What is the output of the following code

int n=0, m; for (m=1; m<=n+1; m++) printf(“%d”, m); (A) 2 (B) 1 (C) 0 (D) 6 Ans B

Q.76 How many times the following loop be executed

{ …

ch = ‘b’;

while(ch >= ‘a’ && ch <= ‘z’) ch++; }

(A) 0 (B) 25 (C) 26 (D) 1 Ans B

Q.77 Which of the following is FALSE in C (A) Keywords can be used as variable names

(B) Variable names can contain a digit

(C) Variable names do not contain a blank space (D) Capital letters can be used in variable names Ans A

(15)

Q.78 int **ptr; is

(A) Invalid declaration (B) Pointer to pointer (C) Pointer to integer (D) none of the above

Ans B

Q.79 A Pixel is _____________________. (A) a computer program that draws picture (B) a picture stored in secondary memory (C) the smallest resolvable part of a picture (D) None of these

Ans C

Q.80 Which number system is usually followed in a typical 32-bit computer?

(A) 2 (B) 10

(C) 16 (D) 32

Ans A

Q.81 Which technology is used in optical disks?

(A) Mechanical (B) Electrical

(C) Electro Magnetic (D) Laser

Ans D

Q.82 Which of the following storage devices can store maximum amount of data?

(A) Floppy Disk (B) Hard Disk

(C) Compact Disk (D) Magneto Optic Disk

Ans B

Q.83 EPROM can be used for

(A) Erasing the contents of ROM (B) Reconstructing the contents of ROM

(C) Erasing and reconstructing the contents of ROM (D) Duplicating ROM

Ans C

Q.84 Memory unit is one part of

(A) Input device (B) Control unit

(C) Output device (D) Central Processing Unit Ans D

(16)

Q.85 MS-WORD is a

(A) system software (B) high level language (C) spread sheet application (D) word processing package Ans D

Q.86 The grammar dialog box can be involved by choosing grammar from _______ menu.

(A) insert (B) file

(C) tools (D) view

Ans C

Q.87 A template stores (A) styles, macros

(B) Auto Text entires, Customized word command Settings (C) graphics, text

(D) All of the above Ans A

Q.88 To return the remainder after a number is divided by a divisor in EXCEL we use the function (A) ROUND( ) (B) FACT( )

(C) MOD( ) (D) DIV( )

Ans C

Q.89 ________ unit controls the flow and manipulation of data and information. (A) Arithmetic logic (B) Central

(C) Middle (D) Control

Ans D

Q.90 Usually, an algorithm will contain a number of procedural steps which are dependent on results of previous steps and is called _______________.

(A) Flowchart (B) Chart

(C) Drawing Chart (D) Food Chart

Ans A

Q.91 The performance of cache memory is frequently measured in terms of a quantity called:

(A) bit ratio (B) nor ratio

(C) no ratio (D) hit ratio

Ans D

(17)

Q.92 In addition to communicating with I/O, the processor must communicate with the ______________ unit.

(A) control (B) memory

(C) Arithmetic (D) process

Ans B

Q.93 Which of the following was not associated with second generation computers? (A) high level procedural language (B) operating system

(C) magnetic core and transistor (D) All of the above were associated Ans D

Q.94 Which of the following number system/code uses only 0s and 1s

(A) decimal (B) octal

(C) hexadecimal (D) none of the above

Ans D

Q.95 A group of characters that has some meaning as a unit is known as a

(A) field (B) file

(C) record (D) word

Ans A

Q.96 The process of production of customer list in alphabetical order falls under the category of (A) editing (B) sorting

(C) updating (D) calculating

Ans B

Q.97 State in case of following statement whether it is true or false? Magnetic drums and disks are quiet similar in operation.

(A) True (B) False

Ans A

Q.98 State in case of following statement whether it is true or false? Ink-jet printers are classified as impact printers.

(A) True (B) False

Ans B

Q.100 A Compiler is _____________________. (A) a combination of computer hardware

(18)

(C) a program which translates from one high-level to a machine level (D) None of these

Ans C

Q.101 When a key is pressed on the keyboard, which standard is used for converting the keystroke into the corresponding bits

(A) ANSI (B) ASCII

(C) EBCDIC (D) ISO

Ans B

Q.102 Which of the following is not an output device? (A) Scanner (B) Printer

(C) Flat Screen (D) Touch Screen

Ans A

Q.103 The memory location address are limited to

(A) 00000 to 9ffff(16) (B) 00001 to 9ffff(16) (C) 00010 to 9ffff(16) (D) 10000 to 9ffff(16) Ans A

Q.104 The programs which are as permanent as hardware and stored in ROM is known as

(A) Hardware (B) Software

(C) Firmware (D) ROMware

Ans C

Q.105 Memory is made up of

(A) Set of wires (B) Set of circuits

(C) Large number of cells (D) All of these Ans C

Q.106 Using Find command in Word, we can search

(A) characters (B) formats

(C) symbols (D) All of the above

Ans D

Q.107 MS-Word automatically moves the text to the next line when it reaches the right edge of the screen and is called

(A) Carriage Return (B) Enter

(19)

Ans C

Q.108 MS-EXCEL can be used to automate

(A) Financial statements, Business forecasting (B) Transaction registers, inventory control (C) Accounts receivable, accounts payable (D) Any of the above

Ans D

Q.109 Command to delete all files and folders is _______________.

(A) Deltree (B) Del

(C) Remove (D) CD

Ans A

Q.110 Machine language is a language

(A) Directly understood by a computer. (B) Which needs to be translated. (C) Which uses mnemonics.

(D) In which programs are written first. Ans A

Q.111 Which of the following device have a limitation that one can only add information to it but cannot erase or modify.

(A) Floppy Disk (B) Hard Disk

(C) Tape Drive (D) CDROM

Ans D

Q.112 Which amongst the following devices can store the maximum amount of data (A) Floppy Disk (B) Hard Disk

(C) Compact Disk (D) Magnetic Optic Disk

Ans B

Q.113 EPROM can be used for

(A) Erasing the contents of ROM. (B) Reconstructing the contents of ROM.

(C) Erasing and reconstructing the contents of ROM. (D) Duplicating ROM.

Ans A

(20)

(A) Abacus. (B) Clock.

(C) Difference . (D) None of these.

Ans A

Q.115 The language that the computer can understand and execute is called (A) Machine Language. (B) Application Software. (C) System Program. (D) Assembly language. Ans A

Q.116 The three sequential function of CPU operation are

(A) Decode, fetch, execute. (B) Execute, decode, fetch. (C) Fetch, execute, decode. (D) Fetch, decode, execute. Ans D

Q.117 Moving process from a main memory to a disk is called

(A) Scheduling (B) Caching

(C) Swapping (D) Spooling

Ans C

Q.118 While running DOS on a PC, the command that is used to duplicate the entire diskette?

(A) COPY (B) DISK COPY

(C) CHK DISK (D) TYPE

Ans B

Q.119 If data is processed as it arrives, this type of data processing is called (A) Real time processing. (B) Batch processing.

(C) Off line processing. (D) Distributed processing. Ans A

Q.120 Client/Server architecture can be used in

(A) LAN (B) WAN

(C) MAN (D) All of these.

Ans D

Q.121 Which of the following is not a programming language?

(A) UNIX (B) LISP

(21)

Ans A

Q.122 The process of retaining data for future use is called

(A) Reading (B) Writing

(C) Storing (D) Coding

Ans C

Q.123 The process of manipulating the data to achieve some meaningful results is called (A) Information handling. (B)Data sharing.

(C) Data distribution. (D)Data processing. Ans D

Q.124 Windows is a(n)

(A) Operating system. (B) User interface

(C) Operating environment. (D) Programming platform. Ans C

Q.125 The register that keeps track of the program during execution (A) Address register (B) Program counter.

(C) Data register (D) Accumulator

Ans B

Q.126 Typical processing speed of Super Computer is of the order of

(A) 100 MIPS (B) 200 MIPS

(C) 300 MIPS (D) 400 MIPS and above

Ans D

Q.127 While using DIR command,_______switch can not be used with it

(A) /P. (B) /W.

(C) /S. (D) /T.

Ans D

Q.128 ________ is not a utility program

(A) Debugger (B) Editor

(C) Spooler (D) Defragmenter

Ans C

Q.129 The access method used for magnetic tape is

(22)

(C) Sequential (D) Indexed Ans C

Q.130 _______ unit coordinates the sequencing of events within the central processor of a computer.

(A) Logic unit. (B) Arithmetic unit.

(C) Storage unit. (D) Control unit.

Ans D

Q.131 Which of the following is not a direct entry input device?

(A) Optical scanner. (B) Digitizer.

(C) Keyboard. (D) Light pen.

Ans C

Q.132 A network, which is used for sharing data, software and hardware among several users of microcomputers, is called

(A) Wide Area Network. (B) Metropolitan Area Network. (C) Local Area Network. (D) Value Added Network. Ans C

Q.133 The instructions / programs that are loaded into main memory when a computer is booted are (A) Internal commands. (B) External commands.

(C) Utility Programs. (D) Loader. Ans A

Q.134 Which of the following types of memory loses data when power is switched off?

(A) Magnetic tape (B) Static Random Access Memory

(C) Magnetic disk (D) CD-ROM

Ans B

Q.135 Which is the latest write-once optical storage media

(A) Digital Page. (B) CD - ROM disk.

(C) WORM disk. (D) Magneto- optical disk.

Ans B

Q.136 The factor that does not affect the storage capacity of a hard disk

(A) Track density. (B) Height of the hard disk drive. (C) Recording density. (D) Number of plates.

(23)

Q.137 The operating system that reads and reacts in terms of actual time is

(A) Batch system. (B) Quick response system.

(C) Real time system. (D) Time sharing system. Ans C

Q.138 Which of the following is a spreadsheet package?

(A) Corel Draw. (B) Wordstar

(C) EXCEL. (D) MS-WORD.

Ans C

Q.139 Find and Replace option is placed under the__________ menu.

(A) Edit. (B) Insert.

(C) View. (D) File.

Ans A

Q.140 Ink-jet printers are

(A) Impact printers (B) Laser printers (C) Non-impact printers (D) Optical printers Ans C

Q.141 A refreshing circuit is required in

(A) SRAM (B) EPROM

(C) DRAM (D) EEPROM

Ans C

Q.142 The first component of the machine language statement is called

(A) Lines of code. (B) Op-code.

(C) Pseudocode. (D) Operators and operands.

Ans B

Q.143 10001 x 101 =

(A) 101101. (B) 1010101.

(C) 100101. (D) 101010.

Ans A

Q.144 In heap files, records are placed in

(A) Random order. (B) Sequential order.

(C) Indexed order (D) Printer-referenced order. Ans A

(24)

(A) Constant data. (B) Variable data (C) Symbolic data. (D) (A) & (C) both Ans A

Q.146 Conditional jump instructions in 8086 are always

(A) Short-jumps ranging from –128 bytes to +127 bytes. (B) Jumps ranging from –256 bytes to +255 bytes. (C) Short-jumps ranging from –64 bytes to +63 bytes. (D) Jumps ranging from –256 to +256 bytes.

Ans A

Q.147 Lotus notes is a

(A) Spreadsheet package (B) Programming tool.

(C) Notepad (D) Groupware.

(25)

PART – II

DESCRIPTIVES

Q.1 Write a C program that reads a fixed point real number in a character array and then displays rightmost digit of the integral part of number. (6)

Ans: A C program to display rightmost digit of the integral part of a read real number:

#include<stdio.h> #include<conio.h> void main() { char a[10]; int i; clrscr();

printf("enter a fixed point real no. : "); scanf("%s",a); i=0; while(a[i]!='.') i++; printf("%c",a[--i]); getch(); }

Q.2 Output the number x = 56.1624 under the following format specification (i) printf (“%7.2f”, x)

(ii) printf (%f”, x) (iii) printf(“8.2e”, x)

(iv) printf(“%e”, x) (4)

Ans: The output of number 56.1624 under various format specification is: (i) 56.16

(ii) 56.162399 (iii) 5.62e+01 (iv) 5.616240e+01

Q.3 Write C statements to read the values of three variables of the type int, float and string and print them if correct data is entered otherwise print “error in input”. (4)

Ans: A C program that read three values and print these if data input is correct otherwise print “error in input”:

#include<stdio.h> #include<conio.h> void main()

(26)

int n; float m; char p;

printf("enter the values of n,m and p");

((scanf("%d %f %c",&n,&m,&p)==3)?printf("correct input"):printf("error in input"));

getch(); }

Q.4 Write a C program that uses ‘for’ construct to find the sum of the following harmonic series for a given value of n and display the sum.

1 + 1/2 +1/3 +… + 1/n (6)

Ans: A C program to find and display sum of given harmonic series is listed below:

#include<stdio.h> #include<conio.h> void main() { int i,n; float sum=0.0; clrscr();

printf("enter the value of n: "); scanf("%d",&n);

for(i=1;i<=n;i++) sum=sum+(float)1/i;

printf("sum of the series upto %d = %f",n,sum); getch();

}

Q.5 Write conditional operators to evaluate the following function y = 2.4x + 3, for x <= 2

y = 3x –5, for x > 2 (4)

Ans:The conditional operator for given problem can be written as y = ((x < = 2)? (2.4 * x + 3): (3 * x + 5)). A program to evaluate this conditional operator is given below:

#include<stdio.h> #include<conio.h> void main() { int x; float y; clrscr();

printf("enter the values of x\n"); scanf("%d",&x);

y=((x<=2)?(2.4*x+3):(y=3*x+5)); printf("y=%f",y);

(27)

Q.6 Write a C program fragment using “do…while” construct to print out even numbers between 10 to 100 making sure that two numbers are written per line. (4) Ans: A C program to print even numbers between 10 to 100 using “do-while” construct:

#include<stdio.h> #include<conio.h> void main() { int i,k; clrscr(); k=10; i=0; do { if(k>10) { printf("%d",k); printf(" "); if(i%2==0) printf("\n"); } i++; k=k+2; }while(k<100); getch(); }

Q.7 Given an integer, write a C program that displays the number as follows: first line : All digits of integer

second line : all except first rightmost digit

third line : all except two rightmost digits . . .

last line : leftmost digit (8) Ans: A C program to display a number as follows:

#include<stdio.h> #include<conio.h> void main() { int n,k,l,i,r,a[100]; clrscr();

printf("enter any integer value"); scanf("%d",&n);

while(n>0) {

printf("%d",n); printf("\n");

(28)

n=n/10; }

//to have space between the digits of the integer k=n; while(k>0) { l=0; n=k; while(n>0) { r=n%10; a[l]=r; l++; n=n/10; } l--; for(i=l;i>=0;i--) { printf("%d",a[i]); printf(" "); } printf("\n"); k=k/10; } getch(); }

Q.8 Describe the output of the following C program fragment

void main () { int k = 0, x = 0; while (k < 25) { if (k % 5 == 0) { x += k; printf(“%d ”, x); } ++k; } printf(“\nx=%d “, x +k); } (6)

Ans: Output of the given program segment is: 05153050

x=75

k=0; test condition of while loop is true; If condition false till k=5 At k=5, (k%5==0) is true so x=x+k so printf prints x=0+5=5; At k=10, (k%5==0) is true so x=x+k so printf prints x=5+10=15;

(29)

At k=20, (k%5==0) is true so x=x+k so printf prints x=30+20=50; k is incremented till 25 where we are out of while loop.

Outer printf statement prints x+k=50+25=75.

Q.9 Shown below is a Floyd’s triangle. Write a C program to print this triangle (8)

Ans: A C program to print Floyd’s triangle upto 91 is given below:

#include<conio.h> void main() { int num=1,k=1,j; clrscr(); while(num<=91) { for(j=1;j<=k;j++) printf(" %d",num++); printf("\n\n"); k++; } getch(); }

Q.10 Given the string “DATA PROCESSING”, write a C program to read the string from terminal and display the same in the following formats.

(i) DATA PROCESSING (ii) DATA

PROCESSING (6)

Ans: A C program that read string “DATA PROCESSING” from the terminal and display that in two formats: (i)DATA PROCESSING (ii)DATA PROCESSING #include<stdio.h> #include<conio.h> void main() { 1 2 3 4 5 6 7 8 9 10 11 … 15 79 … 91

(30)

int i;

char a[50]; clrscr();

printf("enter the string"); gets(a); puts(a); i=0; while(a[i]!=' ') { printf("%c",a[i]); i++; } i++; printf("\n"); while(a[i]!='\0') { printf("%c",a[i]); i++; } getch(); }

Q.11 Write a C program to compute the value of sin function. The loop must terminate when the absolute value of the term is less than 0.00001.

... 7! / x 5! / x 3! / x x x sin x = − 3 + 5 − 7 + (8)

Ans: A C program to compute the sum of sin function is listed below:

#include<conio.h> #include<math.h> void main() { float x,dig,sum; int i,n; clrscr();

printf("\n Enter the value of x : "); scanf("%f",&x);

printf("\n Enter the value of n : "); scanf("%d",&n);

/*convert x into radians*/ x=x*3.1412/180; sum=x; dig=x; for(i=1;i<=n;i++) { dig=(dig*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1));

(31)

sum+=dig; }

printf("\n The sum is : %6.2f",sum); getch();

}

Q.12 Write a switch statement that will examine the value of an integer variable flag and print the

following messages: (6)

It is hot weather; if flag has value 1 It is a stormy weather; if flag has value 2 It is sticky weather; if flag has value 3 It is a pleasant weather; otherwise

Ans: A Program to demonstrate switch statement:

#include<stdio.h> #include<conio.h> void main()

{

int flag;

printf( "Enter any value\n" ); scanf( "%d", &flag );

switch ( flag ) {

case 1:

printf( "It is hot weather!\n" ); break;

case 2:

printf( "It is a stormy weather!\n" ); break;

case 3:

printf( "It is a sticky weather!\n" ); break;

default:

printf( "It is a pleasant weather!\n" ); break;

}

getch(); }

Q.13 Define a structure named ‘student’ containing two fields ‘name’ and ‘marks’. Ans: A structure student:

struct student { char name[10]; int marks[6]; int total; };

(32)

Q.14 Declare an array of structure having 50 elements of student type. (2) Ans: An array of structure:

struct student stu[50];

Q.15 Write an input statement for inputting the marks and the names of 50 students defined as

above. (3)

Ans: Statements for inputting marks of 50 students:

for(i=0;i<50;i++) {

printf("%d. name : ",i+1); scanf("%s",&stu[i].name); printf("\nenter marks :"); scanf("%d",&stu[i].marks); printf("\n");

}

Q.16 Write a complete C program to compute and print the names of those students who have got more than 80 marks. Also print their marks along with their names. (7) Ans: A C program to compute and print names of students who scored more than 80 marks

#include<stdio.h> #include<conio.h> struct student { char name[10]; int marks; }; void main() { int i,n;

struct student stu[50]; clrscr();

printf("enter no. of students"); scanf("%d",&n);

printf("ENTER NAME AND MARKS\n"); for(i=0;i<n;i++)

{

printf("%d. name : ",i+1); scanf("%s",&stu[i].name); printf("\nenter marks :"); scanf("%d",&stu[i].marks); printf("\n"); } for(i=0;i<n;i++)

(33)

if(stu[i].marks>80) { printf("%s",stu[i].name); printf("\t"); printf("%d",stu[i].marks); printf("\n"); } } getch(); }

Q.17 Write a recursive function in C to compute the value of x where n is a positive integer and n

x has a real value. (7)

Ans: A recursive function to compute the value of xn,power(x,n) is given below:

#include<stdio.h> #include<conio.h> void main() { float x,y; int n; clrscr();

printf("enter the value of x : "); scanf("%f",&a);

printf("enter the value of n : "); scanf("%d",&n);

y=power(x,n);

printf("%f raise to power %d = %f",x,n,y); getch();

}

float power(float a, int b) { float k; if(b==1) return(a); else k=a*power(a,b-1); return(k);}

Q.18 Write a function ‘exchange’ to interchange the values of two variables say x and y. Illustrate the use of this function in calling program. Assume that x and y are defined as

global variables. (7)

Ans: A C function exchange to interchange the values of x and y:

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

(34)

#include<math.h> int i,j; int x,y; void main() { i=10,j=20; clrscr();

printf("The values before exchange is i: %d, j:%d\n",i,j);

exchange();

printf("The values after exchange is i: %d, j:%d\n",i,j); printf("\n"); getch(); } void exchange() { int temp; temp = x; x = y; y = temp; }

Q.19 Write a C function that returns 1 if the argument is a prime number and returns 0 otherwise. (7)

Ans: A C function prime(n) described below returns 1 if the argument is a prime number otherwise 0: #include<stdio.h> #include<conio.h> void main() { int n,flag; clrscr();

printf("enter any number"); scanf("%d",&n); flag=prime(n); if(flag==1) { printf("no. is prime"); else printf("%d",flag); getch(); } int prime(n) { int i; for(i=2;i<n;i++) {

(35)

return 0; }

}

return 1; }

Q.20 Write a C function for searching an element in an array of size N. Assume that elements in array are stored in ascending order. (7)

Ans: A C function to search an element in an array of size n:

rec(int a[],int low,int high,int m) { int mid; mid=(low+high)/2; if(low<=high) { if(a[mid]==m) return 1; if(a[mid]>m) rec(a,low,(mid-1),m); if(a[mid]<m) rec(a,(mid+1),high,m); else

printf("element is not present"); }

}

Q.21 Explain the declaration int (*p (char *a))[10]; (3)

Ans: int (*p (char *a))[10];

interpretation of the above statement is:

int (*p (char *a))[10];

char *a :- pointer to character (Character Array) (*p (Character Array))

int (*p (Character Array)) Integer pointer to character array

int (*p (Character Array))[10]

Array of integer pointer to character array

The given statement represent array of int pointer to char array.

Q.22 Suppose a member of a structure is a pointer variable. How can the object of the pointer be accessed in terms of structure variable name and the member name? (3) Ans:

struct rec {

(36)

int *a;}obj;

to use pointer member with structure variable following operator is used. obj->a;

that is ordinary variables are used with dot (.) operator and pointer variables are used with arrow (->) operator.

Q.23 What happens when a pointer to structure is incremented? What danger is associated with this type of operation? (2)

Ans: When pointer to structure is incremented then the pointer points to the next block of memory. As for example

struct rec { int a; char b[10], *c; }; main() {

struct rec *obj;

obj=(struct rec *)malloc(sizeof(struct rec)); obj->a=10;

strcpy(obj->b, “XYZ”); strcpy(obj->c, “ABC”);

printf (“\n%d\t%s\t%s”,obj->a, obj->b, obj->c); //Incrementing pointer to structure

obj++; obj->a=15;

strcpy(obj->b, “PQR”); strcpy(obj->c, “MNO”);

printf (“\n%d\t%s\t%s”,obj->a, obj->b, obj->c); }

In the above program, dynamic memory allocation is used for assigning one block of structure in obj. Later the pointer to structure (obj) is incremented, so that pointer points to the next block of memory.

Danger associated with increment of pointer to structure:

As in the above program structure member is a char pointer. When pointer to structure is incremented then it may results in memory overwrite, because the size for the pointer member is not defined.

Q.24 Distinguish between the following:

(i) Syntactic error and semantic error (ii) Run time error and logical error

(37)

Ans:(i)Syntactic error and semantic error

Syntactic errors also known as compilation errors are caused by violation of the grammar rules of the language. The compiler detects, isolate these errors and give terminate the source program after listing the errors. Some of the common syntactic errors are:

• missing or misplaced ; or }

• missing return type for a procedure

• missing or duplicate variable declaration

Semantic errors are logical errors. If there is a semantic error in a program, it will run successfully, in the sense that the computer will not generate any error messages, but it will not do the right thing. The problem is that the meaning of the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires working backward by looking at the output of the program and trying to figure out what it is doing.

(ii) Run time error and logical error

Run-time errors: Errors such as mismatch of data types or array out of bound error are known as runtime errors. These errors are generally go undetected by the compiler so programs with run-time error will run but produce erroneous results.

Logical errors: These are the errors related with the logic of the program execution. These errors are not detected by the compiler and are primarily due to a poor understanding of the problem or a lack of clarity of hierarchy of operators. Such errors cause incorrect result.

(iii) Compiler and Interpreter

These are two types of language translators.

A compiler converts the source program (user-written program) into an object code (machine language by checking the entire program before execution. If the program is error free, object program is created and loaded into memory for execution. A compiler produces an error list of the program in one go and all have to be taken care even before the execution of first statement begin. It takes less time for execution.

An interpreter is also a language translator that translates and executes statements in the program one by one. It work on one statement at a time and if error free, executes the instruction before going to second instruction. Debugging is simpler in interpreter as it is done in stages. An interpreter takes more time for execution of a program as compared to a compiler. Q.25 The skeletal outline of a C program is shown below:

main() { FILE *p; int a; float b; char c; p = fopen(“sample.dat”, “r”); … fclose(p); }

Read the values of a, b and c from the data file and

display them on the screen

(38)

Ans:#include<stdio.h> #include<conio.h> void main() { FILE *p; int a; float b; char c; p=fopen("sample.dat","r"); fscanf(p,"%d %f %c",&a,&b,&c); printf("a=%d b=%8.2f c=%c", a,b,c); fclose(p); getch(); }

Q.26 How the program design and program efficiency related to each other. (2)

Ans:Design is an important phase in software engineering. Two critical resources used in development process are execution time and memory. The efficiency of a program is measured in terms of these two resources. Inefficiencies in software design can result in complexity that is costly in software maintenance. Program efficiency can be improved with good program design and error free coding.

Q.27 Given the following program

main( )

{ static int a[5] = {10,20,30,40,50}; void find (int *p);

….

find (a);

….

}

void find (int *p) {int j, sum = 0;

for (j=3;j<5;++j) sum +=*(p+j); printf(“sum = %d”, sum);

return; }

(i) What kind of argument is passed to find? (ii) What kind of value is returned?

(iii) What value is displayed by print statement within find? (9) Ans:#include<conio.h>

main()

{ static int a[5]={10,20,30,40,50}; void find(int *p);

(39)

find(a); getch(); } void find(int *p) { int j, sum=0; for(j=3;j<5;++j) sum+=*(p+j); printf("sum=%d",sum); }

(i) a pointer type variable is passed to function find . (ii)The value returned is void.

(iii)The “printf” statement within find will display 90, which is sum of subscript 3 and 4, that is value 40 and 50 in the array a.

Q.28 What is top down design? Write down the steps to breakdown a problem into sub

problems? (8)

Ans: A top-down design is essentially breaking down a system to gain insight into its compositional sub-systems. In a top-down approach an overview of the system is first formulated, specifying but not detailing any first-level subsystems. Each subsystem is then refined in greater detail, sometimes in many additional subsystem levels, until the entire specification is reduced to base elements. In short the top down approach means decomposing of the solution procedure into subtasks. This approach produces a readable and modular code that can be easily understood and maintained.

Steps for breaking a problem into sub-problems involve the following steps:

1. Start with a simple and short statement of the problem. This is top level in the design.

2. In the next level, describe the program as sequence, selection or repetition of main tasks. These tasks are called modules and these are complete in themselves. However these are described in greater details in next level. A module should have just one entry point and exit point.

3. This process is repeated step by step till all modules are refined. This step-wise refinement stops when there are sufficient details to convert that procedure into program.

4. Pseudo code or flowcharts are used to represent the procedure at intermediate steps.

5. Individual modules are tested at each level to ensure that they are working as per requirement.

Q.29 What are the qualities and capabilities of good algorithms? (8) Ans: Every algorithm should have the following five capabilities and qualities:

1.Input: The algorithm should take zero or more input.

(40)

3. Definiteness: Each and every step of algorithm should be defined unambiguously.

4. Effectiveness: A human should be able to calculate the values involved in the procedure of

the algorithm using paper and pencil.

5. Termination: An algorithm must terminate after a finite number of steps.

Q.30 Design an algorithm that accepts a positive integer and reverses the order of its digits. (8) Ans:An algorithm to find reverse of an integer is:

void reversein() { int n,r; printf("enter an integer"); scanf("%d",&n); printf("\nreverse of %d : ",n); while(n>0) { r=n%10; printf("%d",r); n=n/10; } }

Q.31 Given a set of n numbers design an algorithm that adds these numbers and returns the

resultant sum. (8)

Ans: An algorithm to add n numbers:

void main() {

int a[10],n,i,sum=0;

printf("how many nos. u want to enter"); scanf("%d",&n);

printf("enter the nos."); for(i=0;i<n;i++)

{

scanf("%d",&a[i]); sum=sum+a[i];

}

printf("sum of these numbers = %d",sum); }

Q.32 Determine the hierarchy of operations and evaluate the following expression:

kk = 3 / 2 * 4 + 3 / 8 + 3 (4)

Ans: kk=7 because 3/2*4+3/8+3 =1*4+0+3=7; 3/2 evaluates to 1, int/int=int and similarly 3/8 evaluates to 0.

(41)

Q.33 While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If the quantity and price per item are input through keyboard, write a program to calculate the total expenses. (6)

Ans:A C program to calculate the total expenses if the quantity and price per item are input through keyboard is:

#include<stdio.h> #include<conio.h> void main() { int n,i,price,dis,p,q,sum=0; clrscr();

printf("how many items u purchased"); scanf("%d",&n);

i=1;

while(i!=n+1) {

printf("enter the quantity of %d item",i); scanf("%d",&q);

printf("enter price per item"); scanf("%d",&p); if(q>1000) { dis=.1*p*q; price=(p*q)-dis; } else price=p*q; sum=sum+price; i++; }

printf("total bill = %d",sum); getch();

}

Q.34 Write a program to copy input to output, replacing each string of one or more blanks by a

single blank. (6)

Ans: A C program to copy input to output replacing each string of one or more blanks by a single blank is:

#include<stdio.h> #include<conio.h> void main() { int i,j; char a[50],b[50];

(42)

gets(a); i=0; j=0; while(a[i]!=' ') { b[j]=a[i]; j++;i++; } b[j]=' '; j++; i++; while(a[i]!='\0') { while(a[i]!=' '&&a[i]!='\0') { b[j]=a[i]; j++; i++; } while(a[i]==' ') i++; } b[j]='\0'; printf("%s",b); getch(); }

Q.35 How many types of logical operators are there in ‘C’ programming language. (4) Ans: C allows usage of three logical operators:

Operator

Symbol

Example

It evaluates to

AND && exp1 && exp2 True (1) only if both exp1 and exp2 are true; false (0) otherwise

OR || exp1 || exp2 True (1) if either exp1 or exp2 is true; false (0) only if both are false

NOT ! !exp1 False (0) if exp1 is true; true (1) if exp1 is false For example:

(4 == 4) && (5!= 1) evaluates to True (1), because both operands are true. (4 > 1) || (9 < 1) evaluates to True (1), because one operand is true (4>1). !(5 == 4) evaluates to True (1), because the operand is false.

Q.36 Why do we use functions? Give the advance features of functions. (4) Ans: We use functions due to following reasons:

(43)

1. A programmer may have a block of code that he has repeated forty times throughout the program. A function to execute that code would save a great deal of space, and it would also make the program more readable.

2. It is easy to locate and isolate a faulty function. Having only one copy of the code makes it easier to make changes.

3. Another reason for functions is to break down a complex program into logical parts. For example, take a menu program that runs complex code when a menu choice is selected. The program would probably best be served by making functions for each of the actual menu choices, and then breaking down the complex tasks into smaller, more manageable tasks, which could be in their own functions. In this way, a program can be designed that makes sense when read. And has a structure that is easier to understand quickly. The worst programs usually only have the required function, main, and fill it with pages of jumbled code.

4. A function may be used by many other programs. A programmer can use already compiled function instead of starting over from scratch.

Q.37 Write a program to test if a character from the keyboard is a lower case letter. (8) Ans: A C program to test if a character from the keyboard is a lower case letter:

#include<stdio.h> #include<conio.h> #include<process.h> void main() { char a; clrscr();

printf("\nenter any character :\n"); a=getchar();

if(islower(a)>0)

printf("\nit is lower case letter"); else

printf("\nit is not a lower case letter"); getch();

}

Q.38 What are the features of C preprocessor? Give the differences between macros and

functions? (6)

Ans: A pre-processor is a program that processes the source code before it passes through the compiler. It operates under the control of preprocessor directive. These are placed in the source program before the main.

To define a macro, # define statement is used. This statement, also known as macro definition takes the following general form:

(44)

The pre-processor replaces every occurrence of the identifier in the source code by the string. The preprocessor directive definition is not terminated by a semicolon. For example

#define COUNT 100 will replace all occurrences of COUNT with 100 in the whole program before compilation. Similarly we can define small functions with the help of macros. For example, a macro defined as

#define SQUARE(x) (x*x) will calculate square of argument when it is called. This is called macro definition.

Macros Vs Functions:

A macro's definition is expanded into the code each time the macro is encountered in the source code. If your program invokes a macro 100 times, 100 copies of the expanded macro code are in the final program. In contrast, a function's code exists only as a single copy. Therefore, in terms of program size, the better choice is a true function.

When a program calls a function, a certain amount of processing overhead is required in order to pass execution to the function code and then return execution to the calling program. There is no processing overhead in "calling" a macro. In terms of speed, a macro has the advantage. Q.39 Write a function, which takes an array of real numbers and its size as arguments and returns the maximum. Using the above function write a program to read a set of real numbers from the keyboard and find the maximum number in the array. (10) Ans: A C program to read a set of real numbers from the keyboard and find maximum among them using a max function:

#include<stdio.h> #include<conio.h> void main() { int i,n; float a[100]; clrscr();

printf("\nhow many elements u want to enter :\n"); scanf("%d",&n);

printf("\nEnter the elements:"); for(i=0;i<n;i++) scanf("%f",&a[i]); max(a,n); getch(); } max(float a[],int n) { int i; float k,large; large=a[0]; for(i=1;i<n;i++) { if(a[i]>large)

(45)

{ k=a[i]; a[i]=large; large=k; } }

printf("largests element is : %f",large); }

Q.40 Define a pointer? Write a program to assign any number at random to an integer variable k and display the same through pointer. (8)

Ans: A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. The unary or monadic operator & gives the “address of a variable”. The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.

A pointer is declared as follows: int *ptr;

where *ptr is a pointer of int type.

A Program to display the value of an int variable through pointer is listed below”

#include<conio.h> void main() { int k; int *ptr; clrscr(); k=10; ptr=&k; printf("\n Value of k is %d\n\n",k);

printf("%d is stored at addr %u\n",k,&k); printf("%d is stored at addr %u\n",*ptr,ptr); *ptr=25;

printf("\n Now k = %d\n",k); getch();

}

Output of the program is: Value of k is 10

10 is stored at addr 65524 10 is stored at addr 65524 Now k=25.

Q.41 Write a program to read the coordinates of the end points of a line and to find its length. Use a structure variable named ‘line’ to store the relevant information about its end points.

(8) Ans: A Program to read end points of a line and finding its length is:

(46)

#include<conio.h> #include<math.h> struct lyn { int a1; int b1; int a2; int b2; }; void main() { int a,b; float length; struct lyn line; clrscr();

printf("enter the coordinates of end point A"); scanf("%d%d",&line.a1,&line.b1);

printf("\nenter the coordinates of end point B"); scanf("%d%d",&line.a2,&line.b2);

a=line.a1-line.a2; b=line.b1-line.b2;

length=sqrt(pow(a,2)+pow(b,2));

printf("length of the line is %f",length); getch();

}

Q.42 Explain library string functions. Write a program to copy the contents of the string

“HELLO” to another string. (8)

Ans: Using C standard library functions, we can copy, concatenate, compare, and search strings. Some of the important Library string functions are described below:

(i) strcat() Function concatenates two strings together and has the following form: strcat(string1,string2);

When this function is executed, string2 is appended to string1 by removing the null character at the end of string1.

C permits nesting of strcat functions as strcat(strcat(string1,string2),string3);

(ii) strcmp() is the string comparison function defined in string.h header file. It has the following form:.

int strcmp ( const char *s1, const char *s2 );

strcmp will accept two strings. It will return an integer. This integer will either be: Negative if s1 is less than s2.

Zero if s1 and s2 are equal. Positive if s1 is greater than s2.

(47)

Strcmp performs a case sensitive comparison; if the strings are the same except for a difference in case, then they're countered as being different. Strcmp also passes the address of the character array to the function to allow it to be accessed.

(iii) strcpy() function is just like a string-assignment operator which take the following form: char *strcpy ( char *dest, const char *src );

strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.src may be a character array variable or a string constant.

A C program to copy the contents of string “HELLO" to another string:

#include<stdio.h> #include<conio.h> void main() { char str1[20],*str2; int m,i,flag=0,j; clrscr();

printf("enter the 1st string"); gets(str1);

str2="Hello";

printf("enter the index after which u want to insert Hello in 1st : "); scanf("%d",&m); i=0; while(i<=m) { i++; } j=0; while(str2[j]!='\0') { str1[i]=str2[j]; i++; j++; if(str1[i]=='\0') flag=1; } if(flag==1) str1[i]='\0'; printf("%s",str1); getch(); }

Q.43 Explain the following types of errors, which are considered while testing programs. 1. Syntax errors.

(48)

3. Logical errors.

4. Latent errors. (8)

Ans:

(i) Syntax errors:Syntax errors also known as compilation errors are caused by violation of the grammar rules of the language. The compiler detects, isolate these errors and terminate the source program after listing the errors.

(ii) Run-time errors: Errors such as mismatch of data types or array out of bound error are known as runtime errors. These errors are generally go undetected by the compiler so programs with run-time error will run but produce erroneous results.

(iii) Logical errors: These are the errors related with the logic of the program execution. These errors are not detected by the compiler and are primarily due to a poor understanding of the problem or a lack of clarity of hierarchy of operators. Such errors cause incorrect result.

(iv)Latent errors: These are hidden errors which come into the picture only when a particular data set is used. For example, consider the following statement: int z=x/x-y;

An error will occur when x and y are equal. Q.44 Define the functions:

(i) fwrite() (ii) fread() (6)

Ans:

For binary File I/O we use fwrite and fread .

(i) fwrite( ): it write from memory into a file.The declaration is given below:

size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);

(ii)fread( ):It is used for reading into the memory. The declaration is given below:

size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); Both take four arguments. The first argument is the name of the array or the address of the structure you want to write to the file. The second argument is the size of each element of the array; it is in bytes. For example, if we have an array of characters, the size_of_elements is one. sizeof operator can be used get the size of the various datatypes; The third argument tells how many elements we want to read or write; for example, if it is a 100 element array, we will pass 100.The final argument is simply the file pointer we've been using. When fread is used, after being passed an array, fread will read from the file until it has filled the array, and it will return the number of elements actually read.

For example, FILE *fp;

fp=fopen("c:\\test.bin", "wb"); char x[10]="ABCDEFGHIJ";

fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);

References

Related documents

In summary and taking into account the resonance characteristics of the ACUREX plant, the main contribution of this paper, is to improve a gain schedul- ing (GS) predictive

All the figures and tables should be labeled (Times New Roman 11) and included in list of figures and list of tables respectively.

Závažné etické otázky vznikajú aj ohľadom zabezpeče- nia zdravotnej starostlivosti o osoby, ktoré si svojím rizi- kovým správaním alebo nezdravým životným štýlom pria- mo

Conclusion/Summary : Based on available data, the classification criteria are not met...

On June 9, 2003, the Tax Discovery Bureau (Bureau) of the Idaho State Tax Commission issued a Notice of Deficiency Determination (NODD) to [Redacted] (taxpayers), proposing income

Model 1 - The Impact of Severity and Characterisation on Choice of Adviser Table 2 shows results from the cross-classified multinomial logit model, predicting categorical adviser

9.2.1 A medical director with a full time commitment to the operation of the ICU and who is a Fellow of the College of Intensive Care Medicine. The medical director must have

There are many choices of various types of asset management planning software, but finding one that is truly advanced and embeds the financial analysis and reporting component for