Author: BIM Notes
DSA 2021 Old Paper solutions
Q.no 11
1 2 3 4 5 6 7 8 9 10 11 |
public void push(int element) { // Check if stack is full. if (top == (MAX_SIZE - 1)) { System.out.println("Stack Overflow"); } else { top = top + 1; stack[top] = element; System.out.println(element + " pushed into stack"); } } |
Q.no 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class LinkedList { // head of list Node head; /* Linked list Node*/ class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Inserts a new Node at a specific position. */ public void insertAtPosition(int data, int position) { Node new_node = new Node(data); if (head == null) return; Node temp = head; if (position == 0) { new_node.next = head; head = new_node; return; } for (int i = 0; temp != null && i < position - 1; i++) temp = temp.next; if (temp == null || temp.next == null) return; |
Q.no 13 Q.no 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class TowerOfHanoi { public static void main(String[] args) { int numOfDisks = 3; // A, B, C are names of rods towerOfHanoi(numOfDisks, 'A', 'C', 'B'); } // Java recursive function to solve tower of hanoi puzzle static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { System.out.println("Move disk 1 from rod " + from_rod + " to rod " + to_rod); return; } towerOfHanoi(n-1, from_rod, aux_rod, to_rod); System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod); towerOfHanoi(n-1, aux_rod, to_rod, from_rod); } } |
Q.no 15
Continue ReadingDSA 2022 Old Paper Solution
Group B Q.no 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public void deleteFromPosition(int position) { if (head == null) return; Node temp = head; if (position == 0) { head = temp.next; return; } for (int i = 0; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; Node next = temp.next.next; temp.next = next; if (next != null) next.prev = temp; } |
Q.no 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Scanner; import java.util.Stack; public class StackDemo { public static void main(String args[]) { Stack<Integer> st = new Stack<Integer>(); Scanner sc = new Scanner(System.in); System.out.println("Stack Test\n"); char ch; do { System.out.println("\nStack Operations"); System.out.println("1. push"); System.out.println("2. pop"); System.out.println("3. peek"); System.out.println("4. check empty"); System.out.println("5. size"); int choice = sc.nextInt(); switch (choice) { case 1 : System.out.println("Enter integer element to push"); st.push( sc.nextInt() ); break; case 2 : try { System.out.println("Popped Element = "+ st.pop()); |
Q. No 14 Q. No 15
Continue ReadingPHP 2022 Old paper Solution
Q.no 11 A function is a block of code that performs a particular task. Functions allow us to break a complex task into simpler and more manageable parts, making our code easier to read, maintain, and debug. To declare a function, you must use the keyword “function“ followed by the name of the function, followed […]
Continue ReadingPHP 2021 Old paper Solution
Q.no 11
1 2 3 4 5 6 7 8 |
<?php function ascendingOrder($arr){ sort($arr); foreach($arr as $value){ echo $value . " "; } } ?> |
Q.no 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<html> <head> <title>Form Validation</title> <script> function validateForm() { var x = document.forms["form_data"]["name"].value; if (x == "") { alert("Name must be filled out"); return false; } var y = document.forms["form_data"]["age"].value; if (isNaN(y) || y < 1 || y > 100) { alert("Age must be a number between 1 and 100"); return false; } var z = document.forms["form_data"]["comments"].value; if (z == "") { alert("Please enter a comment"); return false; } } </script> </head> <body> <form name="form_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return validateForm()"> Name: <input type="text" name="name"><br><br> Age: <input type="text" name="age"><br><br> Comments: <textarea name="comments"></textarea><br><br> <input type="submit" value="Submit"> </form> </body> </html> |
Q.no 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php // Assumption: You have a database table called "subjects" // with 3 columns: "subject_code", "subject_description", "credit" // Create connection $conn = new mysqli("localhost", "username", "password", "databaseName"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // insert data into subjects table $sql = "INSERT INTO subjects (subject_code, subject_description, credit) VALUES ('ENG101', 'English Composition', '3')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
Q.no 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php if(isset($_POST['submit'])) { $name = $_POST['name']; $age = $_POST['age']; $gender = $_POST['gender']; $file = fopen("Person.txt", "a") or die("Unable to open file!"); fwrite($file, $name.",".$age.",".$gender. "\n"); fclose($file); echo "Data Saved successfully!"; } ?> <html> <head> <title>Person Form</title> </head> <body> <form action="" method="POST"> Name: <input type="text" name="name"/><br/> Age: <input type="text" name="age"/><br/> Gender: <input type="radio" name="gender" value="Male"/>Male <input type="radio" name="gender" value="Female"/>Female <br/> <input type="submit" name="submit" value="Submit"/> </form> </body> </html> |
Q.no 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // Start the session session_start(); // Set Session Variables $_SESSION['name'] = 'John'; $_SESSION['age'] = 25; $_SESSION['city'] = 'New York'; // Retrieve Session Variables echo "My name is " . $_SESSION['name'] . "<br>"; echo "My age is " . $_SESSION['age'] . "<br>"; echo "My city is " . $_SESSION['city'] . "<br>"; ?> |
Continue Reading