0% found this document useful (0 votes)
31 views24 pages

PSTVIVAQUESTIONSJUBAIR

Viva important questions

Uploaded by

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

PSTVIVAQUESTIONSJUBAIR

Viva important questions

Uploaded by

wayseaofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 24
1. What is an algorithm? Provide an example of a simple algorithm. Definition: ‘An algorithm i a step-by-step procedure or set of rules to perform a specific task or solve «problem, Exainple of a Simple Algorthm: Algorithm to add two numbers 1, Start 2, Toke two numbers as input. @ and 8. Add the numbers: sun = a 4b 4, Display the result: sum 5. Stop 2. Algorithm to calculate the sum of all even numbers up to a given number 1, Start 2. Input the number 3. Intiaize sum = 0 4, Loop through numbers from 110 + Hf the number is even, ad itto sun 5: output sum 6. Stop 3. What is a flowchart, and how does it help in algorithm design? Definition: ‘flowchart isa dlagrammatic representation of an algorithm using symbols ike ovals, rectangles, and diamonds to depict steps, processes an decision making How ithelps + Provides a visual representation ofthe process + Makes the logic easy to understand, + Identifies errars or inefficiencies inthe algorithm, 4. C Code Snippet: Use of if-else Statements B coco ‘inelude int main() { Aint number: printf("Enter a nunber: *); scant ("%d",, Sumber) Af (number % printf¢ pelse { pprintf("Rd is odd. \n", number); ¢ is even.\n", number): , return 0; ‘5. Purpose of goto Statement in C with Example Purpose ‘The (goto statement allows contro to jump to a labeled statement in the program. It's generally discouraged but can be usefUl for breaking nested loops or error handling. Example: e O copy cose finelude Ant main() { int i = 0; abel bel a 4 printf(rsd *, 4); aes 4 goto label; to the label > 6. Difference Between struct and union inC Feature Stuct Union ‘Storage Eachmemberhasits oun memory _Almembers share the ame memory. Size Sumof sizes ofall members ‘Sie ofthe largest member, Use Use whenallmembersare needed at Use when only one members needed at once, tine, 7. C Program to Find Factorial of a Number include int main() { Ant num, factorial = 1 printf(*Enter 2 nunber scanf("%d", Roun) for (int i factorial * i; fA <= mum; 344) 4 printf("Factorial of 4 is Xd\n", mum, factorial); return 8. Role of Pointers in Dynamic Memory Allocation Pointers are used in dynamic memory allocation to allocate and menage memory during runtime. Functions ike mallee, €a1l6e, and realloe return pointers tothe allocoted memory, 9. Define Linear Search and Explain Its Steps with Example Definition: Linear search is a method to find an element in an array by sequentially checking each clement unt the desired value i found othe list ends steps: 1. Stat from the first element 2. Compate the element withthe target 3. sf they match, retum the index 4, fro, move tothe next element 5. Repeat unt the element is found or thelist ends, Example in ¢ ° 8 copy coe #include int main() ¢ int arr) = 41, 2, 3, 4, Sb int n= S, target = 4, found = 0; for (int i= 0; i int fibonacci(int n) 4 if (ne 4 return n; > return fibonacci(n - 1) + fibonacci(n - 2); int main() int n; printf("Enter the position: scanf("Kd", &n); printf("Fibonacci number at position sd is d\n", return 0: . fibonacci(n)) 11. Purpose of Switch Statements in C with Example Purpose The ‘switch statement simplifies multiple conditional checks by allowing selection from multiple cases, Example: e 8 cony code #include int main() { int choice; printf("Enter a number (1-3): scanf("%d", &choice); switch (choice) { case 1 printf("You chose one.\n"); break: case 2 printf("You chose two.\n"); break; case 3 printf("You chose three.\n"); break; default: printf Invalid choice. \n"); > return 0; 12. Function of continue Statement in Loops with Example Function The ‘continue statement skips the current iteration of the loop and proceeds to the next iteration Example 2 8 copy cose include int main() { for (int 1 = 1; i <= 5; itt) { if G continue; // Skip when i is + printf("%d } return 0; 1. What is a flowchart? Describe its components. A flowchart is a diagram that represents a process or algorithm using symbols. It is widely used to visually display the steps of a program or process, making it easier to understand Components of a flowchart: ‘+ Oval (Terminator): Represents the start or end of a process + Rectangle (Process): Represents a task or operation * Diamond (Decision): Represents a decision-making step with outcomes like Yes/No + Arrow: Shows the flow of the process 2. Write an algorithm to compute the average of a set of numbers. Algorithm: 1. Start. 2. Initialize sum = 0 3. Input the total number of numbers, n 4, For each number, input the value and add it to. sum 5. Compute average = sum /n 6. Print average 7. Stop, 3. What is a variable in C? Explain with an example. Avariable in C isa named memory location used to store data. Its value can change during program execution Example: #include int main() { int age = 25; jeclaratio i printf("Age is Xd", age); return 0; 4, Write a C program to calculate the factorial of a number using recursion. Code: B 0o0p) code #include int factorial(int n) { if (n==0 [jn return 1; 0 else return n* factorial(n - 1), int main() { int num; printf("Enter = number: ") scanf("%d", rum); printf("Factorial of Sd is %d", num, factorial(num)); return 0; 5. Explain the use of for loops with a simple example in C. A for loop is used to repeat a block of code a specific number of times. Example: B00 coe #include int main() { for (int i= 1; i <= 5; ies) { printf("sd ", i) ? return 0; ‘Output: 12345 6. What is a pointer? How does it differ from an array in C? Apointer isa variable that stores the memory address of another variable. Difference between Pointer and Array: ‘+ Apointer stores the address of a variable, whereas an array is a collection of elements of the same data type stored sequentially in memory. + Apointer can point to different memory locations, but the base address of an array is. fixed, 7. Write a C program to reverse an array. Code: ° Bi copy code Hinclude int maing) { int n, i, temp; printf("Enter size of array: "); seanf("kd", &n); int arr[n printf("Enter %4 elenents: ", 1); for (i = 0; i struct Student { int rollNo; char name[50]; float marks; int main() { struct Student st = {1, “John Doe", 85.5}; printf("Roll No: %d, Name: %s, Marks: %.2f", s1.rolINo, st.name, stn return 0; 8. Define and give an example of a structure in C. Definition: A structure in C is a user-defined data type that groups together related variables (of different types) into a single unit. Structures are used to represent complex data more efficiently. ‘Syntax: struct StructureName { data_type member1; data_type member2; / Additional members 3: Example with Code: #include struct Student { int rollNo; / Memb char name[50]; // Me float marks; / Mer int main() { struct Student s1 = {1, printf("Roll No: %d\n", printf("Name: %s\n", s1 printf("Marks: %.2f\n", return 0; "John Doe", 90.5}; s1.rollNo); «name ) ; si.marks); © Copy code GP Copy code 9. What is the purpose of break in C? Provi Definition: ide an example. ‘The break statement in Cis used to exit aloop or switch statement immediately, skipping any remaining erations or cases. Example with Code: #include int main() ¢ for (int i= 1 if G = 3) ¢ break; // Exits the loop whe + printf("%d ", i); > return 0; + Output: i 10. Describe the linear search algorithm and implement it. Definition: copy code write aC program to Linear search is a simple search algorithm that checks every element in a list sequentially Until it finds the target value or reaches the end of the list. ‘Steps of Linear Search: 1, Start from the first element. 2, Compare the target element with each element in the array. 3, If a match is found, return the position, 4, If no match is found, return -1 Code Example: winclude int linearSearch(int arr[], int n, int key) { for (int i= 0; i return A i k Cony code int main() { int n, key: printf("Enter array size: "): scanf("sd", &n); int arr{n); printf("Enter %d elements: ", n); for (int i i printf("Enter the element to search: "); scanf("sd", akey); int result = linearSearch(arr, n, key); if (result 1-1) ¢ printf("Element found at index %d\n", result); yelse { printf("Element not found\n"); y return 0; 11. What is a nested loop? Provide an example in C. Definition: Anested loop in Cis a loop inside another loop. The inner loop executes completely every time the outer loop executes once. Example with Code: e copy code #include int main() for (int i for (int j = 1; j <= 2; j+#) { // Inner loop printf("i = %d, j = d\n", i, 4): 3; is) { // outer Loop return 0; Output: oss B 0py code 12. Explain how arrays are declared and initialized in C. Definition: An array in C is a collection of elements of the same data type, stored in contiguous memory locations. Arrays can be declared and initialized during declaration or later in the program. Syntax for Declaration: © GF Copy code data_type array_name{size]; ‘Syntax for Initialization: © 6B Copy code data_type array_name[size] = {value1, value2, Code Example: @ Copy code #include int main() { int arr[5] = {10, 20, 30, 40, 50}; // Declaration and Initializat printf("Array elements are:\n"); for (int i = 0; i <5; i++) { printf("arr[%d] = %d\n", i, arr[il); + return 0; + Output: os py code arr[0] = 10 arr[1] = 20 arr[2] = 30 arr[3] = 40 arr[4] = 50 1. What is the significance of algorithms in computer science? Definition: ‘An algorithm is a step-by-step procedure or formula for solving a problem. in computer science, algorithms are essential because they provide a clear set of instructions for solving specific tasks, enabling efficient and effective programming. Significance: ‘Provides a structured approach to solving problems. ‘+ Helps in optimizing performance and resource usage. ‘+ Forms the backbone of software design and development. + Enables reusability and scalability. 2. Write an algorithm to find the largest of three numbers. Algorithm: 1. Start 2. Input three numbers, a, b,and ¢ 3, Compare the numbers: + if'a> band a>, print a asthe largest. * Elseif b > a and b > c, print b as the largest. « Else, print ¢ as the largest. 4, Stop. 3. What is a while loop in C? Provide an example. Definition: [A while loop in C repeatedly executes a block of code as long as the given condition is tue. Example: ° Copy code #include int main() ¢ int = 1; while (i <= 5) { as long as i printf("ad ", i); > return 0; Output: 12345 4, Write a C program to perform summation of a set of numbers using alloop. Code: Winelude int maing) ¢ int n, sum = 0, num: printf("Enter the number of elenents: *); scanf(-ud", an) for Cint 4 = 0; 4 printf(-The sum is: d\n", sum); return 0 5. Explain the difference between if and switch statements with examples. Difference: ‘+ Lf is used for conditional execution of code blocks based on logical expressions. + switch is used to execute one black of code out of many, based on the value of a variable. Example of if Winelude Ant sainc) ¢ int rum = 5; Af (rum > 0) ¢ printf("Positive nunber\n ): p else { printf(*Non-positive nunber\n"); > return 0 Example of switch Hinelude int mainc) { int day = 3; switch (day) ¢ case 1: printf¢*monday\n"); break: case 2: printf("Tuesday\n"); break; case 3: printf(*Wednesday\n"); break; default: printf(“invalid day\n") > return 0 6. What is a matrix? How can matrices be represented in C? Definition: ‘A matrix is @ two-dimensional array of numbers arranged in rows and columns. Representation in C: A matrixis represented as a 2D array, ; CB copycode include int maing) int matrix(2}[3] = 441, 2, 3h, (4, 5, 6D for (int i = 0; 4 < 2; ive) ¢ for (int J = 0; $< 3; J4) ¢ printfcrad *, matetxCi3t31): ? printf(-\n") > return 0; 7. Write a C program to demonstrate the use of pointers with functions. include void increnent(int *num) { num +43 int main) int a = 10; printf(*Before increment: d\n", a): increment (a); printf(“After increment: d\n", a)? return 0; 8, Describe the purpose of continue in loops with an example. Definition: The continue statement skips the current iteration of a loop and moves tothe next Iteration Example: ¢ om #include int maing) ¢ for (int 4 na 45; ier) a4 id= ‘continue; // Skips the iteration whe 8 copy cose > printfcrad ", i); > return 0 Output: 124.5 9. What is the purpose of using the goto statement in C? Definition: The goto statement allows jumping toa labeled part ofthe program, often used to simplify complex nested loops or error handling. Example: e copy cone ‘include int naing ¢ int num = -1; Af (rum < 0) ¢ goto error; > printf("Nunber is positive. \n") return 0; printf("Error: Negative nunber.\n"); return 1 10. Write a C program to implement counting sort. Code: e copy coce #inelude void countingsort(int arr{], int n, int max) { int count{max + 1], sortedtn]; for (int i = 0; 4 <= max; i++) count{i] = 0; for (int 4 = 0; 4 = 0; 4--) sorted{--countfarr[i]}] = are(i] for (int 1 = 0; 1 int main() { int arr[3] = (10, 20, 30}; for (int i = 0; i <3; itt) { printf("arr(%d] = %d\n", i, arrli]); > return 0; 12. What are command-line arguments in C? Provide an example. Definition: Command-line arguments are parameters passed to the program at the time of execution via the command line. Example: Hinclude int main(int arge, char *argv[]) { printf("Number of arguments: %d\n", argc) for (int i = 0; i < arge; itt) { » i, argv[i); printf("Argument %d: %s\ > return 0; Executi If the program is named prog., run it as: -/prog argi arg2 Output: yar copy code Number of arguments: 3 Argument 0: ./prog Argument 1: argt Argument 2: arg2 v 1. What is the role of an algorithm in problem-solving? Definition: An algorithm is a systematic, step-by-step approach to solving a problem in a finite number of steps. It plays a vital role in problem-solving by: * Breaking down complex problems into manageable steps. + Providing a logical sequence to reach the desired output. + Ensuring correctness and efficiency of solutions. + Serving as a blueprint for writing code. 2. Write an algorithm for reversing a string. Algorithm: 1. Start 2. Input the string str of length 3. Initialize two pointers, start at and end at n-1 4. Repeat the following until start is less than end + Swap the characters at start and end * Increment start by1 * Decrement end by1 5. Output the reversed string. 6. Stop. 3. What is the difference between do-while and for loops? Feature do-while Loop for Loop Condition Condition is checked after executing Condition is checked before executing ‘Check the loop body (post-test). the loop body (pre-test). Execution Executes the loop body at least once. May not execute if the condition is false initially Usage Suitable when the loop must execute Suitable when the number of iterations at least once. Is known beforehand, Example of do-while : e Copy code #include int main() ¢ Example of do-while : #include int main() { inti do { printf("xd ", 1); i; } while (i <= 5); return 0; Example of for : A Cony code #include int main() { for (int i= 1; i <= 5; it+) { printf("xd ", i); } return 4. Write a C program to find the sum of an array of integers. Code: Copy code #include int main() { int n, sum printf("Enter the size of the array: "); scanf("%d", &n); int arrtn]; printf("Enter %d elements:\n", n); for (int i= 0; i int main) { Ant choice; printf("enter a nunber (1-3): " scanf("%d", &choice); ‘switch (choice) { case 1 printf(*You chose One.\n") break; case 2. printf(*You chose Two.\n"); break; case 3: printf(*You chose Three. \n"); break; default printf(*Invalid choice.\n"); > return 0; 6. What is the function of the sizeof operator in C? Provide an example. Definition: The sizeof operator in C is used to determine the size (in bytes) of a data type or variable, Example: . CP copycoae Hinelude int main() { int x; double y; printf(’size of int: Slu bytes\n", sizeof(x)); printf("size of double: ‘lu bytes\n", sizeof(y)); printf("Size of array: lu bytes\n", sizeof(int) * 5); return 0; Output (platform-dependent): Size of int: 4 bytes Size of double: & bytes Size of array: 20 bytes 1. Define an algorithm. Why are algorithms important in computing? Definition: An algorithm is a step-by-step procedure for solving a specific problem or performing a computation in a finite number of steps. Importance in Computing: * Ensures structured problem-solving. * Provides efficiency in processing and resource management * Acts as a blueprint for implementing programs. * Facilitates debugging and optimization 2. Write a simple algorithm to compute the factorial of a number. Algorithm: 1. Start 2. Input the number n 3. Initialize factorial = 1 4, Foreach i from1to n « Multiply factorial by i 5. Output factorial . 6. Stop 3. Explain the purpose of flowcharts in algorithm design. Purpose of Flowcharts: * Visually represent the logic and flow of an algorithm. * Simplify understanding of complex processes, + Help in identifying logical errors or inefficiencies * Provide documentation for future reference. 4. Write a C program snippet to swap two variables using a temporary variable. Code: ‘ Copy cote #include int main() { int a= 5, b= 10, temp; printf("Before swap: a wd\n", a, b) temp = a arb; b = temp printf("after swap: a = %d, b = %d\n", a, b); return 0; 5. What is the difference between while and do-while loops in C? Feature do-while Loop Condition Concition is checked after the loop Check body (pretest). body (post-test) Execution Maynot execute ifthe condition is_—_—Executes atleast once regardless of false intial, the condition Use Case Suitable for cases where prior Suitable for cases where execution Condition evaluation is requited must happen at least once 6. Explain the role of pointers in C programming with a simple example. Role of Pointers: + Store the memory address of variables. + Enable dynamic memory allocation + Facilitate passing by reference in functions. + Access arrays and other data structures efficiently. Example: e Bony cous Hinclude int main() { Example: include int main() ¢ int x = 10; int *ptr = &x; // Pointer to the meno printf("Value of x: d\n", x); printf ("Value of x using pointer: xd\n", *ptr); return 0; 7. What is a structure in C? Provide a simple example. Definition: A structure in C is a user-defined data type that groups related variables of different data types into a single unit Example: © CB cony cose #include struct Student { int roll; float marks; int main() ¢ struct Student st = {1, 85.5}; printf("Roll: %d, Marks: %.2f\n", s1.roll, s1.marks); return 0; 8. Describe the function of the break statement in loops. Definition: The break statement is used to terminate a loop prematurely, skipping the remaining iterations. Example: ¢ copy coe Winclude int main() { for (int i it int main() { for (int i= 1; i <= 5; it+) ¢ if (i == 3) ¢ break; // Exit hen i is 3 > printf("ad *, 4); > return 0; } Output: 12 9. Write a C program to compute the sum of the first 10 natural numbers. Cod CB Copy code #include int main() { int sum for (int i sum += i <= 10; itt) { + printf("Sum of the first 10 natural numbers: %d\n", sum); return 0; 10. What is the purpose of the continue statement in C? Definition: The continue statement skips the remaining code in the current iteration of a loop and moves to the next iteration, Example: Cony cove #include int main() { for (int i= 1; i <= 5; i*4) ( if (i == 3) ¢ continue; // Skip the iteration whe 3 + printf("ad ", 1); #include int main() { for (int i= 1; i <= 5; it) { if (== 3) ¢ continue; » the itera 7 > printf("%d ", i); + return 0; Output: 12.45 11. Define linear search and explain its basic concept. Definition: Linear search is a simple algorithm that checks each element of an array sequentially to find the target element. Basic Concept: 1. Start from the first element of the array. 2. Compare each element with the target 3. If amatch is found, retum the index. 4. If no match is found, return -1 12. What is the difference between a one-dimensional array and a two-dimensional array? Feature One-Dimensional Array Two-Dimensional Array Structure Contains a singlerow of elements, Contains rows and columns of elements Declaration int arr([5) dint arr(3) (4); Usage Suitable for near data likelists, Suitable for tabular data like matrices Example of One-Dimensional Array: int arr(3] = (1, 2, 3}; Example of Two-Dimensional Array: © Coy code int arr{2{3] = {{1, 2, 3}, {4, 5, 6h}:

You might also like