Skill(s) that is/are needed by programmer's _______________________.
- APaying attention to detail
- BThink about the reusability
- CThink about user interface
- DAll of the given options
The course lists all these as required programmer skills.
Syllabus: lectures 1–24. Past-paper MCQs are shown first.Syllabus: lecture 1–24. Past paper ki MCQs pehle dikhai gayi hain.
184 MCQs · 15 short · 12 long questions · 184 MCQs from past papers184 MCQs · 15 short · 12 long sawal · 184 MCQs past papers se
Skill(s) that is/are needed by programmer's _______________________.
The course lists all these as required programmer skills.
In year ____, C was approved as a standard language worldwide.
ANSI approved the C standard in 1989 (ANSI C / C89).
The C language was developed in late 60's and early 70's, in ____.
Dennis Ritchie developed C at AT&T Bell Laboratories.
C is widely known as development language of _______ operating system.
C became known as the language in which UNIX was written.
UNIX has been developed in ________ language.
UNIX was rewritten in C, which was created for it.
There are mainly -------------------- types of software
Software is mainly system software and application software.
Compiler is a
A compiler is a programming tool classed as system software.
Operating System is
The operating system is system software.
In computer systems there are mainly ___________ type of softwares.
Software is mainly of two types: system software and application software.
What will be the result of arithmetic expression 6+48/4*3?
/ and * go left to right first: 48/4 = 12, 12*3 = 36, then 6 + 36 = 42.
In the given expression which operator will be evaluated first? 10 + (6 / 2) - 2 * 3?
The expression inside parentheses, 6 / 2, is evaluated first.
What will be the value of the variable output in the given piece of code? double output = 0; output = (2 + 2) * 4 + 2 / (4 - 2);
(2+2)*4 = 16 and 2/(4-2) = 1, so output is 17.
In if structure the block of statements is executed only,
The if block runs only when its condition evaluates to true.
What will be the value of variable "input" if the initial value of input is 67? if(input >= 50) input = input + 1; if(input <= 75) input = input + 2; else input = input - 1;
67 >= 50 gives 68; 68 <= 75 adds 2, giving 70.
When the if statement consists more than one statement then enclosing these statement in curly braces is,
Without braces only the first statement belongs to the if.
A ___________ structure specifies that an action is to be repeated while some condition remains true.
A repetition (loop) structure repeats an action while a condition is true.
When break statement is encountered in switch statement, it ______
break transfers control to the statement after the switch block.
If the break statement is missed in switch statement then,
Without break, control falls through into the next case, which can cause a logical error.
What is the output of the following code, if the first case is true switch (var) { case 'a':cout<<"apple"<<endl; case 'b':cout<<"banana"<<endl; case 'm':cout<<"mango"<<endl; default: cout<<"any fruit"<<endl; }
Without break, all statements from case 'a' onward run.
_________statement is used to terminate the processing of a particular case and exit from switch structure.
break ends the current case and leaves the switch.
The return type of a function that do not return any value must be ________
void declares that a function returns no value.
If the return type of a function is void then it means that it will,
void means the function returns no value.
What is the highest legal index for the following array? int arr[4]
Indexes run from 0 to size-1, so the last index is 3.
Which of the following syntax will be used to initialize an array of size 5 of int data type to value 0?
An initializer list {0} sets the first element to 0 and the remaining elements are zero-initialized; the declaration needs the type.
In C/C++ if we define an array of size eight (8) i.e. int Arr [8]; then the last element of this array will be stored at,
Indexes start at 0, so the last of 8 elements is Arr[7].
Suppose a program contains an array declared as int arr[100]; what will be the size of array?
The declared size is 100 elements (indexes 0 to 99).
There is a pointer variable named ptr of type int then address of which type of variable the ptr will store in it?
An int pointer stores the address of an int.
Dereferencing operator is represented by _______
The unary * operator dereferences a pointer.
What will be the correct syntax for initialization of pointer ptr of type int with variable x?
A pointer to int is declared with * and initialized with the address of x.
The statement cout << yptr will show the __________the yptr points to.
Printing the pointer itself shows the address it holds.
_________ is used as a dereferencing operator.
The unary * operator gives the value at the address in a pointer.
These functions seekg() and seekp() require an argument of type ____________ to let them know how many bytes to move forward or backward.
The offset argument of seekg()/seekp() is a long value (stream offset), as taught in the handouts.
We have opened a file stream myfile for reading (getting), myfile.tellg() gives us the current get position of the file pointer. It returns a whole number of type___________
tellg() returns the position as a long (stream position) value per the handouts.
A structure brings together a group of ____.
A structure is used to group related variables that can be of different data types.
To access the data members of structure _______ is used.
Structure members are accessed with the dot operator.
Dealing with structures and functions passing by reference is the most economical method
Passing a structure by reference avoids copying all of its members.
A structure is a collection of _____________ under a single name.
A structure groups related variables under one name.
Most efficient method of dealing with structure variables is to define the structure globally
A globally defined structure type is visible to all functions that use it.
Which of the following is used with bit manipulation?
Bitwise operations are applied to integers, typically unsigned ones.
If most significant bit of un-signed number is 1 then it represents a positive number.
An unsigned number has no sign bit, so its value is never negative.
A macro when takes arguments is called ______.
A #define macro that takes arguments is a parameterized macro.
Which of the following is NOT a preprocessor directive?
#ndefine does not exist; #error, #define and #line are valid directives.
If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.
The preprocessor substitutes the constant text, so PI cannot change at run time.
All preprocessor directives are started with the symbol______.
Every preprocessor directive begins with #.
We can also do conditional compilation with preprocessor directives.
#if, #ifdef, #ifndef, #else and #endif provide conditional compilation.
Where we can include a header file in the program?
#include can appear anywhere, though it is normally placed at the top.
Which one is the correct syntax for defining an identifier PI with preprocessor directive?
A #define uses name and replacement text with no = and no semicolon.
In C/C++ the #include is called,
Lines starting with # are preprocessor directives.
Symbolic constant PI can be defined as:
#define takes the name and replacement text with no = and no semicolon.
Which of the following is the correct way to declare a variable x of integer type?
A declaration gives the type first, then the name: int x;
Reserve words cannot be used as a variable name.
Keywords are reserved by the language and cannot be identifiers.
What will be the range of numbers generated by function rand () % 9?
The remainder after division by 9 is always from 0 to 8.
Which of the following is the correct function call having array named student of 10 elements as a parameter.
An array is passed by its name only, which gives the address of its first element.
Declaring structures does not mean that memory is allocated.
A structure declaration only defines a type; memory is used when a variable is created.
Identifier is a name that can be given to variables, labels and functions.
Identifiers are user-chosen names for variables, functions and labels.
Truth tables are used for analyzing ___________.
Truth tables list results of logical expressions for all input combinations.
What does 5 ^ 6 , evaluate to in decimal where '^' is Exclusive OR operator?
101 XOR 110 = 011, which is 3.
Array is passed by value to a function by default.
An array name passes the address of its first element, i.e. by reference.
break statement can be used outside a loop or switch statement.
break is only allowed inside a loop or switch.
The keyword_______ is used to return some value from a function.
return sends a value back to the caller.
Here the code is given below. You have to identify the problem in the code. while( (i < 10) && (i > 24))
No value can be both less than 10 and greater than 24, so the condition is always false.
Which one of the statements given below is equivalent to sum += j*j ?
x += e means x = x + e, so sum += j*j is sum = sum + j*j.
All elements of an array must be of ________.
An array is a collection of elements of one single data type.
In C/C++, null character is represented as ________.
The null character that ends a C string is written '\0'; '\n' is newline.
The address operator (&) is used with a ________.
& gives the memory address of a variable (an lvalue), not of a constant or expression.
A variable declared inside a code block becomes the ________ variable for that block.
A variable declared inside a block has local scope limited to that block.
A ________ name includes a primary name and an optional period with an extension.
A file name has a primary name and an optional extension after a period, e.g. data.txt.
Individual characters in a string stored in an array can be accessed directly using array ________.
Array elements, including characters of a string, are accessed with a subscript (index).
Which of the following function call is "call by reference" for the following function prototype? void func (int *);
Passing the address &num matches an int* parameter, so the function can change num.
Which of the following is the correct way to initialize a character array 'course' with string 'programming'?
"programming" has 11 characters plus the null terminator, so 12 elements are needed; the other forms are invalid syntax.
______ executes all the lines before error and stops at the line which contains the error.
An interpreter translates and runs one line at a time, so it stops at the first line with an error.
Which of the following values are used in C/C++ to represent true and false?
C/C++ uses 1 (non-zero) for true and 0 for false.
What will be the output of this code segment? int arr[6] = {2, 3, 7, 4, 5, 6}; int *ptr1 = &arr[1]; int *ptr2 = &arr[4]; cout << (ptr2-ptr1);
Subtracting pointers gives the number of elements between them: 4 - 1 = 3.
In case of ______ default mechanism of function calling is call by value.
Ordinary variables are passed by value by default, while arrays are passed by reference.
In Flow Chart, flow of control is represented by ____.
Arrows (flow lines) show the direction in which control moves between flowchart symbols.
The operators ++ and -- are used to increment or decrement the value of a variable by ____.
Increment and decrement operators change the value by exactly 1.
To assign a value to a character type variable, ____ are used around the value.
A char literal is written inside single quotes, e.g. 'a'.
Let ptr1 and ptr2 are pointer variables that point to integer data types, then which one of the following arithmetic is allowed?
Subtracting two pointers into the same array gives the number of elements between them; adding, multiplying or dividing pointers is not allowed.
When a call to a user-defined function finishes, the variable defined inside the function will ____.
Ordinary local (automatic) variables are destroyed when the function returns.
You can use a ________ statement to transfer control elsewhere out of the nested structure.
break leaves only the innermost loop/switch; goto can jump out of several nested structures at once.
Which of the given operators is not a valid compound assignment operator?
=>> is not a C++ operator; >>=, *= and &= are compound assignment operators.
Assignment operator is used for ___________.
= stores the value on its right into the variable on its left; == checks equality.
Missing semicolon ';' at the end of C++ statement causes a ___________ error.
A missing semicolon breaks the language grammar, so the compiler reports a syntax error.
Which of the following is not an example of int data type?
-4.0 has a decimal point, so it is a floating-point (double) literal, not an int.
For which array, the size of the array should be one more than the number of elements in an array?
A char array holding a string needs one extra element for the terminating null character '\0'.
A Pointer is a special variable that contain
A pointer holds the memory address of another variable.
Computer can understand only machine language code.
The processor executes only machine code; other code must be translated.
What will be the value of 'a' and 'b' after executing the following statements? a = 3; b = a++;
Post-increment gives b the old value 3, then a becomes 4.
Consider the following code segment. What will be the output of following code? int addValue (int *a){ int b = (*a) + 2; return b ; } main () { int x =6 ; cout << x << "," ; cout << addValue(&x) << "," ; cout << x ; }
addValue returns 8 without modifying x, so x prints 6 before and after.
_______ is used to trace the logic of the program and correct the logical errors.
A debugger lets you step through code to find logical errors.
Assignment operator is -------------------------associative.
a = b = c is evaluated as a = (b = c), so assignment is right-associative.
Header file: fstream.h includes the definition of the stream classes __________.
fstream defines the file stream classes ifstream, ofstream and fstream.
Which of the following is correct way to initialize a variable x of int type with value 10?
Initialization gives the value in the declaration itself; option a declares then assigns.
Consider the following code segment. What will be the output of the following program? int func(int) ; int num = 10 ; int main(){ int num ; num = 5 ; cout << num ; cout << func(num) ; } int func(int x){ return num ; }
main prints its local num 5; func returns the global num 10.
What will be the correct syntax to declare two-dimensional array of float data type?
Each dimension of a C++ array gets its own pair of square brackets after the name.
The number 544.53 must be stored in _____ data type.
A value with a fractional part needs a floating-point type.
For which values of the integer_value will the following code becomes an infinite loop? int number=1; while (true) { cout << number; if (number == 3) break; number += integer_value; }
Starting at 1, number reaches exactly 3 only with step 1 or 2; any other step skips 3 and never breaks.
*.doc is _____________ by type.
A word-processor document is read and written at arbitrary positions, so it is a random access file.
C++ is a case-sensitive language
C++ treats Sum and sum as different identifiers.
What will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr?
An array name converts to the address of its first element, which is assigned directly to ptr.
What will be the correct syntax to access the value of fourth element of an array using pointer ptr?
ptr[3] and *(ptr+3) both read the element at index 3.
If it is required to copy an array to another array then,
Element-by-element copying expects arrays of the same type and size.
If there is more than one statement in the block of a for loop, which of the following must be placed at the beginning and the ending of the loop block?
A compound statement (block) is enclosed in braces.
The operator used for casting, in C, is standard ________ operator.
C uses the cast operator (type) to convert values.
A pointer variable can be,
Pointer arithmetic allows increment and decrement but not multiplication.
NULL value has been defined in ______ and _________ header files.
NULL is defined in stddef.h and also made available by stdlib.h.
When an array is passed to a function then default way of passing this array is,
An array name passes the address of its first element, so the function works on the original array.
Default mechanism of function calling in case of array is _____ and in case of variable is ___.
Arrays are passed by their address while ordinary variables are copied.
The programs, in which we allocate static memory, run essentially on ________
Statically sized local data is placed on the stack, not the heap.
&& is -------------------- operator.
&& is the logical AND operator.
Which of the following function(s) is/are included in ctype.h header file?
isdigit, isxdigit and tolower are all character-handling functions in ctype.h.
Which character is inserted at the end of string to indicate the end of string?
C strings end with the null character '\0'.
The object _______________may be used both for file input and file output
fstream supports both reading and writing.
The functions used for dynamic memory allocation return pointer of type ______
malloc, calloc and realloc return void*.
seekg() and write() are functionally _________________ .
seekg moves the get position while write outputs bytes.
When a pointer is incremented, it actually jumps the number of memory addresses
Incrementing a pointer advances it by the size of its pointed-to type.
The name of an array represents address of first location of array element.
An array name decays to a pointer to its first element.
If Num is an integer variable then Num++ means,
++ increments the variable by 1.
Which looping process checks the test condition at the end of the loop?
do-while tests its condition after executing the body.
In a group of nested loops, which loop is executed the most number of times?
The innermost loop runs fully for each iteration of the outer loops.
Consider the following statements to initialize a two-dimensional array. (i) int arr[2][3] = {4, 8, 9, 2, 1, 6} ; (ii) int arr[3][2] = {4, 8, 9, 2, 1, 6} ; (iii) int arr[][2] = {{4,8},{9, 2},{1, 6}} ; Which of the following option(s) are correct to initialize a two-dimensional array with 3 rows and 2 columns?
Both (ii) and (iii) give 3 rows of 2 columns; (i) has 2 rows of 3.
While developing a program, should we think about the user interface?
A good program design considers how users will interact with it.
A character is stored in the memory in _________
A char occupies one byte of memory.
When the logical operator OR (||) combines two expressions exp1 and exp2, then the result will be false only,
|| yields false only when both operands are false.
Suppose we have int y[10]; To access the 4th element of the array we write_________
Indexing starts at 0, so the 4th element is y[3].
ofstream is used for________
ofstream is the output file stream class.
In C/C++ the string constant is enclosed
String literals are written inside double quotes.
In flow chart process is represented by
A rectangle denotes a process step in a flowchart.
When we are using const keyword with a variable x then initializing it at the time of declaration is,
A const variable cannot be assigned later, so it must be initialized when declared.
Which of the following is the correct way to assign an integer value 5 to element of a matrix say 'm' at second row and third column?
With zero-based indexing, second row is index 1 and third column is index 2.
How many total elements must be in two-dimensional array of 3 rows and 2 columns?
3 rows x 2 columns = 6 elements.
Which of the following is the correct statement for the following declaration? const int *ptr;
The const applies to the pointed-to int, not to the pointer.
Consider the following code segment. What will be the output of this code segment? int arr[6] = {2, 3, 7, 4, 5, 6} ; int *ptr1 = &arr[1] ; int *ptr2 = &arr[4] ; cout << (ptr2-ptr1) ;
Pointer subtraction gives the element distance 4 - 1 = 3, not the difference of values.
C is a/an ______ language
C is a procedural language built around functions, not objects.
The variables having a name, type and size are just like empty boxes.
A declared variable is like a labelled box that can hold a value of its type.
What's wrong with this for loop? for (int k = 2, k <=12, k++)
The three parts of a for header are separated by semicolons.
Syntax of union is identical to ______
A union is declared with the same syntax as a struct.
If we do not mention any return_value_type with a function, it will return an _____ value.
In old C/C++ the omitted return type defaults to int.
Let suppose int a, b, c, d, e; a = b = c = d = e = 42; This can be interpreted by the compiler as:
Assignment is right-associative, so it groups from the right.
Missing semicolon ';' at the end of C++ statement is
The compiler rejects a statement without its terminating semicolon.
An array stores the numbers into consecutive memory locations.
Array elements are stored contiguously.
A variable which is defined inside a function is called
Local variables are automatic by default.
The code is written to __________ the program.
Writing code is the implementation stage.
Analysis is the -------------- step in designing a program.
Analysis of the problem comes first.
In flow chart, the symbol used for decision making is,
A diamond represents a decision in a flowchart.
The correct syntax of do-while loop is,
do-while puts the body first and ends with while (condition);
C++ views each file as a sequential stream of ________________ .
A file is treated as a sequence of bytes.
If the elements of an array are already sorted then the useful search algorithm is,
Binary search needs sorted data and halves the range each step.
Each pass through a loop is called a/an
One execution of a loop body is an iteration.
Call by reference mechanism should be used in a program when there is i. large amount of data to be passed ii. small amount of data to be passed iii. need to change the passed data iv. no need to change the passed data Choose the appropriate option for the above case.
Reference avoids copying large data and lets the function change the caller's data.
Which of the following is the starting index of an array in C++?
C++ arrays are zero-indexed.
Which of the following is an extension of header file?
Header files conventionally use the .h extension.
We want to access array in random order which approach is better?
Indexing is better for random access; pointers suit sequential access, per the handouts.
When we declare a multidimensional array the compiler stores the elements of multidimensional array in the form of,
All elements are laid out in one contiguous block (row-major).
We can ___________ pointer.
A non-const pointer can be incremented, decremented and made to point elsewhere.
char **argv can be read as__________________.
Read right to left: argv is a pointer to a pointer to char.
To read command-line arguments, the main() function itself must be given ___________ arguments.
main(int argc, char **argv) takes two arguments.
The increment of a pointer depends on its___________.
Pointer arithmetic moves by the size of the pointed-to type.
Transpose of a matrix means that when we interchange rows and columns___________.
In a transpose, row i becomes column i, so the first row becomes the first column.
We can define a matrix as__________ array.
A matrix has rows and columns, so it is stored as a two-dimensional array.
A ________is an array of characters that can store number of character specified.
A C-style string is a char array of a fixed size.
Given a two dimensional array of integers, what would be the correct way of assigning the value 6 to the element at third row and fourth column?
Indexes start at 0, so third row is 2 and fourth column is 3.
________ of a variable means the locations within a program from where it can be accessed.
Visibility (scope) is where in the program a variable can be used.
Which of the following function call is "call by reference" for the following function prototype? int add (int *);
A pointer parameter needs the address of the variable, &x.
Which of the function call is call by value for the following function prototype? float add(float);
Passing the variable name sends a copy of its value.
______ will be used for enclosing function statements into a block.
Curly braces mark the start and end of a block.
When the break statement is encountered in a loop's body, it transfers the control ________ from the current loop.
break exits the loop and continues after it.
What will be the output of following code segment? for (int i = 2; i<10; i++){ if ( i == 5) continue; cout << i << "," ; }
continue skips printing only when i is 5; the loop runs 2 to 9.
What will be the result of the expression k = ++m; if initially k = 0 and m = 5?
Pre-increment makes m 6 first, then assigns 6 to k.
How many times the following do-while loop will execute? int k = 10; do { cout << "Statements" << endl; k -= 2; } while(k>0);
The body runs for k = 10, 8, 6, 4, 2; then k is 0 and the loop stops: 5 times.
Which of the following loops checks the test condition at the end of the loop?
do-while tests its condition after the body, so it runs at least once.
How many times the following loop will execute? int j = 3; while(j > 0) { cout << "Statements" << endl; j -= 2; }
The body runs for j = 3 and j = 1; then j is -1 and the loop ends.
!( x > 3) means in C++ that
Not (x > 3) is the same as x <= 3.
When the logical operator && combines two expressions then the result will be true only when the both expressions are ____________
&& is true only when both operands are true.
< and > both are _________ operators.
< and > compare two values, so they are relational operators.
!( x < 3) means in C++ that
Not (x < 3) is the same as x >= 3.
!= operator is used to check whether the operand on the left-hand-side is __________ to the operand on the right-hand-side.
!= tests whether two values are not equal.
The most suitable data type for number 325.25 is ______.
A number with a fractional part needs a floating-point type.
Which of the following will be the most appropriate data type to store the value 63.547?
A decimal value is stored in a floating-point type.
____________ operators are the ones that require two operands on both sides of the operator.
Binary operators take two operands.
It is the job of __________ to transfer the executable code from hard disk to main memory.
The loader copies the executable program into memory to run it.
_________ will explain the function of a program.
Comments describe what the code does for human readers.
If we open a file stream myfile for reading, what will give us the current position of the file pointer?
tellg() returns the current get (read) position of an input stream.
Which one of the following operators is a unary operator?
~ works on a single operand; ||, && and ^ need two.
No MCQs match this filter.Is filter mein koi MCQ nahi.
Identify and correct the error(s) in given code. while ( c <= 5 ); { sum + = c; c++; }
Error: the semicolon after while ( c <= 5 ) makes an empty loop body, so the block never runs inside the loop and the loop never ends (infinite loop) when c <= 5. Also + = must be written without a space as +=, and c and sum must be initialized. Corrected: int c = 1, sum = 0; while ( c <= 5 ) { sum += c; c++; }
What will be the value of the given expression after evaluation if x = 3 and y = 2? (++x*++y)-2
10. Pre-increment makes x = 4 and y = 3 before use, so (4*3) - 2 = 12 - 2 = 10.
Consider the following code segment. What will be the output of the following code segment? main(){ char string1[] = "introduction"; char string2[] = "programming"; cout << "String 1 is " << string1 << endl; cout << "Size of string 1 is " << sizeof(string1) << "bytes" << endl; cout << "String 2 is " << string2 << endl; cout << "Size of string 2 is " << sizeof(string2) << "bytes"; }
String 1 is introduction Size of string 1 is 13bytes String 2 is programming Size of string 2 is 12bytes (sizeof counts the characters plus the terminating null: 12+1 = 13 and 11+1 = 12.)
What will be the output of given program code? main() { char province[] = "Sindh"; char s[80]; strcpy(s, "Hyderabad"); strcat(s, " and "); strcat(s, province); cout << s; }
Hyderabad and Sindh (strcpy copies "Hyderabad" into s, the first strcat appends " and ", the second appends "Sindh".)
Which bit of the number is used as a sign bit?
The most significant (leftmost) bit is the sign bit: 0 means the number is positive and 1 means it is negative (for signed types).
Determine the output for code given below. main() { int x = 5 ; { int x = 10 ; cout << x ; cout << endl ; } cout << x ; }
10 5 The inner block declares a new x = 10 that hides the outer x, so 10 is printed followed by a newline. After the block ends the inner x goes out of scope and the outer x (5) is printed.
Find out error in the code given below: if ( num % 2 = 0 ) cout << "The number is even" << endl;
The condition uses the assignment operator = instead of the equality operator ==. num % 2 is not an lvalue, so this is a compile error. Correct code: if ( num % 2 == 0 ) cout << "The number is even" << endl;
How is learning to design programs like learning to play soccer?
A soccer player first learns basic skills (trapping, dribbling, passing, shooting), then learns positions and strategies, chooses among them and sometimes invents a new variation when none fits. Similarly, a programmer first learns the basic language constructs, then learns standard design techniques and strategies, chooses the right one for a problem, and adapts or creates a new design when no known one fits.
What is difference between pointer and variable?
A normal variable stores a data value (e.g. int x = 5; stores 5). A pointer variable stores the memory address of another variable (int *p = &x;). The value is reached through the pointer with the dereference operator *p, and pointers support address arithmetic.
What is language translator?
A language translator is a program that converts source code written in a high-level language (like C/C++) into machine language that the computer can execute. Types: compiler (translates the whole program at once into an object/executable file), interpreter (translates and executes line by line), and assembler (translates assembly language to machine code).
Which arithmetic operators cannot have a floating point operand?
The modulus operator % works only with integer operands; e.g. 7.5 % 2 is a compile error (use fmod() for floating point remainders).
Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;
int matrix[3][3] = { 0 }; // all 9 elements are 0 or with loops: int matrix[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) matrix[i][j] = 0;
What is the difference between switch statement and if statement.
An if statement can test any condition (relational/logical expressions, ranges, floating point values) and chooses between two paths (more with else-if). A switch statement compares one integer/char expression against a list of constant case values and jumps to the matching case; it is cleaner for multi-way choice on a single value, but cannot test ranges or floating values, and without break execution falls through to the following cases.
Which bitwise operator returns true if both bits are different and returns false if both bits are the same?
The bitwise exclusive OR (XOR) operator ^ : 1^0 = 1, 0^1 = 1, 0^0 = 0, 1^1 = 0.
Assume that you write a program to read the data from a text file. You have two options to do it, either read the data character by character or reading multiple lines of data at a time. Which option do you think is more better and why?
Reading multiple lines (a large block) at a time is better. Every read operation on a disk file is slow compared with memory access; reading a large block into a buffer in one operation needs far fewer I/O calls than reading each character separately, so the program runs much faster. The data can then be processed in memory. Character-by-character reading is only preferable when each character must be examined as it is read and speed is not important.
Construct a logical expression to represent each of the following conditions: a. score is greater than or equal to 80 but less than 90; b. answer is either N or n; c. n is even but not 8; d. number is a multiple of both 4 and 6
a. (score >= 80 && score < 90) b. (answer == 'N' || answer == 'n') c. (n % 2 == 0 && n != 8) d. (number % 4 == 0 && number % 6 == 0)
Write a program which declares and initializes an integer array of 10 elements and then using a pointer, displays the array elements in reverse order.
#include <iostream> using namespace std; int main() { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr = &arr[9]; // point to the last element for (int i = 0; i < 10; i++) { cout << *ptr << " "; ptr--; } cout << endl; return 0; }
Write the procedure of data insertion in the middle of a file by the Merge Method practiced in older systems.
1. Open the original data file and a new empty file. 2. Read the data file from its beginning. 3. Copy the data read into the new file until the position where the new data is to be inserted is reached. 4. Write (append) the new data into the new file. 5. If data is being replaced, skip that part of the original file. 6. Copy the remaining part of the original file to the end of the new file. 7. Close both files; the new file (renamed if needed) replaces the old one.
Write a recursive function that takes three arguments (an integer array, starting subscript 's' and ending subscript 'e'). In the first recursive call, the function should display the array from subscript 's' (s = 0) to 'e' (e = size of array). In each successive call, the function should print the array from index s+1 to e. The function should stop processing and return when the starting subscript becomes equal to the ending subscript. For example, if user enters values for array 2, 3, 4, 5, 6 then the recursive function must display the following output. 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6
#include <iostream> using namespace std; void printArray(int arr[], int s, int e) { if (s == e) // base case: nothing left to print return; for (int i = s; i < e; i++) cout << arr[i] << " "; cout << endl; printArray(arr, s + 1, e); // recursive call from next index } int main() { const int SIZE = 5; int arr[SIZE]; cout << "Enter " << SIZE << " integers: "; for (int i = 0; i < SIZE; i++) cin >> arr[i]; printArray(arr, 0, SIZE); return 0; }
Identify each of the following as system software and application software: LINUX, DISK CLEANUP, WORD PROCESSOR, WINDOWS, STUDENT INFORMATION (system).
System software: Linux, Windows (operating systems) and Disk Cleanup (a system utility). Application software: Word Processor and Student Information System (they perform user tasks).
Why binary search algorithm is efficient than linear search algorithm?
Linear search checks elements one by one from the start, so in the worst case it needs n comparisons (1000 for 1000 elements). Binary search works on a sorted array: it compares the key with the middle element and then discards half of the remaining array each time (divide and conquer). The number of comparisons grows only as log2(n), so 1000 elements need at most about 10 comparisons and 1,000,000 need about 20. Hence binary search is far more efficient for large sorted data (its requirement is that the array must be sorted).
Write a program in C++, which will prompt the user to input 8 values for a 2x4 matrix. Then display those values in 2x4 matrix format. /* Program Output will look like this: Enter eight values for a 2x4 Matrix: 1 2 3 4 5 6 7 8 Matrix entered in its original matrix format: 1 2 3 4 5 6 7 8 */
#include <iostream> using namespace std; int main() { int matrix[2][4]; cout << "Enter eight values for a 2x4 Matrix:" << endl; for (int i = 0; i < 2; i++) for (int j = 0; j < 4; j++) cin >> matrix[i][j]; cout << "Matrix entered in its original matrix format:" << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) cout << matrix[i][j] << "\t"; cout << endl; } return 0; }
What is the difference between an array and a structure?
An array is a collection of elements that all have the same data type; its elements are stored one after another and are accessed by an index (subscript), e.g. a[3]. A structure (struct) groups related variables that can have different data types under one name; its members are accessed by name with the dot operator, e.g. s.name (or -> through a pointer). An array name works like a constant pointer to its first element, while a structure variable is a single object that can be assigned and passed as a whole. Example: int marks[5]; versus struct Student { char name[30]; int age; float gpa; };
Write a program that inputs one string of max length of 10. If the string is identical to "Virtual", output the message "Correct Password"; otherwise, output the first four characters in the message and the length of the message.
#include <iostream> #include <cstring> using namespace std; int main() { char str[11]; // max 10 characters + '\0' cout << "Enter a string (max 10 characters): "; cin.getline(str, 11); if (strcmp(str, "Virtual") == 0) { cout << "Correct Password" << endl; } else { cout << "First four characters: "; for (int i = 0; i < 4 && str[i] != '\0'; i++) cout << str[i]; cout << endl; cout << "Length: " << strlen(str) << endl; } return 0; }
Write code that will declare, initialize, and fill in an array of objects of type int. After your code executes, the array should look as follows. 0 2 4 6 8 10 12 14 16 18
#include <iostream> using namespace std; int main() { int arr[10] = {0}; // declare and initialize for (int i = 0; i < 10; i++) arr[i] = i * 2; // fill: 0 2 4 ... 18 for (int i = 0; i < 10; i++) cout << arr[i] << " "; cout << endl; return 0; }
Write a program that uses a function multiple(int,int) that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.
#include <iostream> using namespace std; int multiple(int first, int second) { if (first == 0) // avoid division by zero return second == 0 ? 1 : 0; return (second % first == 0) ? 1 : 0; } int main() { int a, b; char more = 'y'; while (more == 'y' || more == 'Y') { cout << "Enter two integers: "; cin >> a >> b; if (multiple(a, b)) cout << b << " is a multiple of " << a << endl; else cout << b << " is not a multiple of " << a << endl; cout << "Another pair? (y/n): "; cin >> more; } return 0; }
Write the statements that will a) declare a one-dimensional integer array with 8 elements b) initialize each element in the array to 0 c) prompt the user for 8 integers and store those integers in the array d) find the largest value in the array (use a loop)
// a) and b) int arr[8]; for (int i = 0; i < 8; i++) arr[i] = 0; // c) for (int i = 0; i < 8; i++) { cout << "Enter integer " << i + 1 << ": "; cin >> arr[i]; } // d) int largest = arr[0]; for (int i = 1; i < 8; i++) if (arr[i] > largest) largest = arr[i]; cout << "Largest value: " << largest << endl;