Practice Questions: Section 1 Q1- Complete missing parts of the following code according to designed GUI and explained functions of the GUI components. ******** ******** Button Functions: ******** • • • • When the Enter Record button is pressed, all the values in the textboxes should be concatenated (combined or joined together) and added to listbox. In addition, a messagebox should appear on screen and says “record added successfully”. When the Clear button is pressed, all entered values on the form should be cleaned. When the Remove button is pressed, selected item of the listbox should be removed from listbox. When the Exit button is pressed, program should terminate. Listbox Function: • When any item is selected from listbox, a message should appear on the screen and says “Selected item: …”. using System; using System.Collections.Generic; using System.ComponentModel; 1 using using using using using using System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms; namespace ExamQuestion1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void ... (object sender, EventArgs e) // To Enter Record to the Listbox { ... ... ... ... ... ... ... ... } private void ... (object sender, EventArgs e) // To Terminate Program { Application.Exit(); } private void ... (object sender, EventArgs e) // To Remove Selected Item Form Listbox { ... ... } private void ... (object sender, EventArgs e) // To Clear Form { ... ... ... ... } private void ... (object sender, EventArgs e) // To Display on Screen, Selected Item of Listbox { ... } } } Q2- Write Source code for the following designed Windows Form Application using the provided data for available books in the bookstore. At first, Book List should be empty and after pressing List Books button, list 2 of books will be appeared on the treeview component according to categories and types. In addition, if the user set collapse all, records of the treeview will be collapse and it will be expanded for the expand all. GUI Components’ Names: • • • TreeView: o treeViewBookList Radiobuttons: o rdbtnCollapse o rdbtnExpand o Buttons: o Set o ListBooks o Exit Button Functions: • • • When the List Books button is pressed, all the avaliable books according to their categories and types will be on the treeview. Initially, treeview will be empty and data will be populated when the List Books button is pressed. When the Set button is pressed, according to selected option, either treeview will be collapsed or expanded. When the Exit button is pressed, program should terminate. Database: Readers Book Store Book Database • Scheme: o Categories: ▪ Types: • Available Items: o Programming Books ▪ C#.Net • Deitel & Deitel C# • Microsoft Visual C# ▪ Java • Java in a Nutshell o Database Management Books ▪ SQL Server • T-SQL • SQL PL in SQL Server ▪ MySQL • MySQL GUI Administration • SQL PL in MySQL Operating System Books o using System; using System.Collections.Generic; 3 using using using using using using using System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms; namespace ExamQuestion2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ... ... ... ... ... ... ... ... } } // End of Program 4 Q3 - The following program is not working properly. Check entire code and make necessary correction to make it working properly and show steps and generate output values using different input values that shows program working in the correct way. GUI Components’ Names: • • Buttons: o btnCalculate o btnExit TextBoxes: o txtBoxNumeric o txtBoxLetter The above program allows users to calculate their letter grade based on their numeric grade. The only needed operation is here to enter numeric grade and program calculates the letter grade and displays it in the textbox Letter Grade. The letter grade assessment is listed below. • • • • Letter Grade A: Equals to or greater than 80 Letter Grade C: Equals to and greater than 60 and less than 80 Letter Grade F: Equals to and greater than 40 and less than 60. Letter Grade NG: Less than 40 Source Code for Button Calculate: ... { String numericGrade; numericGrade = txtBoxNumeric.Text; if(numericGrade > 80) { txtBoxNumeric.Text = A; } else if (numericGrade >= 60 || numericGrade <= 80) { txtBoxLetter.Text = C } else (numericGrade > 40 || numericGrade < 60) { txtBoxLetter.Text = F; } else { txtBoxLetter.Text = NG; } } ... 5 Section 2 Q18- Trace and find output for the following source code. (Show the steps how the code works). int[] numbers = { 5,3,6,2,4,8 }; double sum = 0.00; double average = 0.00; int min = 9999999; int max = numbers[0]; for (int i = 0; i < numbers.Length; i++) { sum = sum + numbers[i]; } average = sum / numbers.Length; for (int i = 0; i < numbers.Length; i++) { if (max < numbers[i]) max = numbers[i]; } for (int i = 0; i < numbers.Length; i++) { if (min > numbers[i]) min = numbers[i]; } MessageBox.Show(“Sum: “ + sum.Tostring() + “\n” + “Count: “ + numbers.Length.Tostring() + “\n” + “Average: “ + average.Tostring() + “\n” + “Max: “ + max.Tostring() + “\n” + “Min: “ + min.Tostring() + “\n”) Output: 6 Q19- Trace and find output for the following source code. (Show the steps how the code works). bool result, a = true, b = false; result1 = a & b; MessageBox.Show("Result1: " + result1.ToString()); Result2 = a | b; MessageBox.Show("Result2: " + result2.ToString()); Result3 = a ^ b; MessageBox.Show("Result3: " + result3.ToString()); Result4 = !a; MessageBox.Show("Result4: " + result4.ToString()); Output: Q20- Trace and find output for the following source code. (Show the steps how the code works). int input; switch (input) // what is input? { case 0: case 1: MessageBox.Show("You typed break; case 2: MessageBox.Show("You typed break; case 3: MessageBox.Show("You typed break; default: MessageBox.Show("You typed break; } 1 (one)"); 2 (two)"); 3 (three)"); a number other than 1, 2 and 3"); Output: If input = 0; Output: If input = 1; Output: If input = 2; Output: If input = 10; Output: Section 1: Q1: 7 ******** ******** ******** namespace ExamQuestion1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void enterrecord_Click(object sender, EventArgs e) { string fullname, phoneno, age, result; fullname = txtBoxFullName.Text; phoneno = txtBoxPhoneNo.Text; age = txtBoxAge.Text; result = fullname + " " + phoneno + " " + age; listBoxUserFullList.Items.Add(result); MessageBox.Show("Record Added Successfully!"); } private void exit_Click(object sender, EventArgs e) { Application.Exit(); } private void remove_Click(object sender, EventArgs e) { 8 listBoxUserFullList.Items.RemoveAt(listBoxUserFullList.SelectedIndex); MessageBox.Show("Record Removed Successfully!"); } private void clear_Click(object sender, EventArgs e) { listBoxUserFullList.Items.Clear(); txtBoxFullName.Clear(); txtBoxPhoneNo.Clear(); txtBoxAge.Clear(); } private void listBoxUserFullList_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("Selected Item: " + listBoxUserFullList.SelectedItem.ToString()); } } } Q2: namespace ExamQuestion2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked) treeViewBookList.CollapseAll(); else if (radioButton2.Checked) treeViewBookList.ExpandAll(); else MessageBox.Show("Error!!!"); } 9 private void btnListBooks_Click(object sender, EventArgs e) { treeViewBookList.Nodes.Add("Programming Books"); treeViewBookList.Nodes.Add("Database Management Books"); treeViewBookList.Nodes.Add("Operating System Books"); treeViewBookList.Nodes[0].Nodes.Add("C#.Net"); treeViewBookList.Nodes[0].Nodes.Add("Java"); treeViewBookList.Nodes[1].Nodes.Add("SQL Server"); treeViewBookList.Nodes[1].Nodes.Add("MySQL"); treeViewBookList.Nodes[0].Nodes[0].Nodes.Add("Deitel & Deitel C#"); treeViewBookList.Nodes[0].Nodes[0].Nodes.Add("Microsoft Visual C#"); treeViewBookList.Nodes[0].Nodes[1].Nodes.Add("Java in a Nutshell"); treeViewBookList.Nodes[1].Nodes[0].Nodes.Add("T - SQL"); treeViewBookList.Nodes[1].Nodes[0].Nodes.Add("SQL PL in SQL Server"); treeViewBookList.Nodes[1].Nodes[1].Nodes.Add("MySQL GUI Administration"); treeViewBookList.Nodes[1].Nodes[1].Nodes.Add("SQL PL in MySQL"); } } } Q3: private void btnCalculate_Click(object sender, EventArgs e) { int numericGrade; numericGrade = Convert.ToInt32(txtBoxNumeric.Text); if(numericGrade>=80) { txtBoxLetter.Text = "A"; } else if (numericGrade >= 60 && numericGrade < 80) { txtBoxLetter.Text = "C"; } else if (numericGrade >= 40 && numericGrade < 60) { txtBoxLetter.Text = "F"; } else { txtBoxLetter.Text = "NG"; } } Section 2: Q1: 10 Q2: Q3: private void button1_Click(object sender, EventArgs e) { int[] numbers = { 5, 3, 6, 2, 4, 8 }; double sum = 0.00; 11 double average = 0.00; int min = 9999999; int max = numbers[0]; for (int i = 0; i < numbers.Length; i++) { sum = sum + numbers[i]; } average = sum / numbers.Length; for (int i = 0; i < numbers.Length; i++) { if (max < numbers[i]) max = numbers[i]; } for (int i = 0; i < numbers.Length; i++) { if (min > numbers[i]) min = numbers[i]; } MessageBox.Show("Sum: " + sum.ToString() + "\n" + "Count: " + numbers.Length.ToString() + "\n" + "Average: " + average.ToString() + "\n" + "Max: " + max.ToString() + "\n" + "Min: " + min.ToString() + "\n"); } private void button2_Click(object sender, EventArgs e) { bool result, a = true, b = false; result = a & b; MessageBox.Show("Result1: result = a | b; MessageBox.Show("Result2: result = a ^ b; MessageBox.Show("Result3: result = !a; MessageBox.Show("Result4: " + result.ToString()); " + result.ToString()); " + result.ToString()); " + result.ToString()); } private void button3_Click(object sender, EventArgs e) { int input; input = Convert.ToInt16(textBox1.Text); switch (input) // what is input? { case 0: case 1: MessageBox.Show("You typed break; case 2: MessageBox.Show("You typed break; case 3: MessageBox.Show("You typed break; default: MessageBox.Show("You typed break; } 1 (one)"); 2 (two)"); 3 (three)"); a number other than 1, 2 and 3"); } 12