Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 14 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
14
Dung lượng
14,7 KB
Nội dung
50. You program needs to used the App class present in the foo.bar package. Which of the following statements imports the class to your program? Answer: import foo.bar.* 51. Consider the following statements: Statement A: String is a primitive data type to represent a sequence of characters. False Statement B: You can declare a String variable as: String str = "I am a String"; . True 52. Consider the following statements: Statement A: Operator || returns true if one of the operands is true. True Statement B: Operator || returns false if both operands are false. True 53. Consider the following class declaration: class Example { /*class body*/ } Which the following statements is true regarding the preceding class Answer: The Example class is accessible to the classes within the same package. 54. Consider the following statements: Statement A: When your class extends a super class, your class inherits all the public methods of the super. True Statement B: When your class extends a super class, your class inherits all the constructors of the super. False 55. Sam is writting a Java code where he needs to declare an integer array that can hold 10 element. Which of the following is the correct declaration for Sam's requirement? Answer: int [10] numbers; 56. Class x and class y are the sub classes of class A. Class A has a method defined as accessMe().Class x is within the same package as Class A where as class y is in some other package to class x but not to class y. What is the access specifier defined for accessMe() method? Answer: Friendly 57.Which the following is NOT a valid sitch statement? Answer: int num 3; switch (num) { case 3: System.out.println("Case 3"); continue; case 1: System.out.println("Case 1"); continue; case 2: System.out.println("Case 2"); continue; case 4: System.out.println("Case 4"); continue; 58. In a Java Program. umArray is an array intialized with int value. Which of the following statement/ statements, will you use to print th last int value stored in the array? Answer: System.out.println(numArray[numArray.Length - 1]); 59. Consider the following code: public class Example { public static void main(String[] args){ try{ String name = args[1]; System.out.println (name); } Select the correct option on executing the code with the following command java Example James Answer: the code executes without any output 60. Consider the following code: import java.util.*; public class Example { public static void main (String [] args){ <Class Name> obj = new <Class Name>l obj.add("Alps"); obj.add("Alps"); System.out,println (obj); } Which the following class will you use in the place of <Class Name> ti execute the program with the following input: [Alps] Answer: HashSet 61. Consider the following code: public class Example { public static int num1 = 4; public int num2 = 5; } In the preceding code, which one is a class variable? Answer: the num1 variable 62. Consider the following statements: Statement A: There can be multiple catch block after a try block. True Statement B: The finally clause defines a block of code that is always executed irrespective of an exception being thrown or not. True 63. A class Shape has a method draw(). Three classes, Rectangle, Square and Circle will be derived from the Shape class. What will you do to force the subclasses to override the draw() method? Answer: Specify Shape as an interface and Rectangle, Square, and Circle should implement the Shape class 64. What will be the output of the following code snippet: Class illegalValueException extends Exception { Public String getMessage() return “wrong values supplied”; Class user { Int val1, val2; Public user(int a, int b) { val1 = a; val2 = b; } Void show() throws illegalValueException { If ((val1<0) || (val2 > 0)) throw new illegal Value Exception(); System.out.println (“Value 1 = “ + val1); System.out.println (“Value 2 = ” + val2); } } Public class trial { Public static void main (String args[]) { User u1 = new user(-2,20); Try { U1.show() } Catch (illegalValueException e) { System.out.println (e,getMessage()); Answer: “wrong values supplied”. gets displayed. 65. A: An Error class is derived from an Exception class. False B: Error class is used for VirtualMachineError. True 66. William has written the following program in java. Public class student { Public static void man (String args[]) { Int array[] = {0,0}; Int num1 = 100; Try {System.out.println(num1/array[1]);} Catch(ArrayIndexOutOfBoundsException e) {System.out.println(“Error… Out of bounds”);} } Output: Arithmetic exceptions gets raised 67. Wich of the following combinations of access specifiers CANNOT be used with the variables of an interface? Protected and private 68. A: An interface can be used to simulate multiple inheritance. True B: A Class can implement several interfaces True C: All the variable of an interface are static, final and public. True D: All the methods of an interface are abstract. True 69. What will be the output of the following code? Import java.awt.*; Public class Example { Private Franmef; Private button b1, b2, b…6; (từ b1 đến b6) Public Example() { F = new Frame (“ridLayout Demo”); B1 = new Button(“1”); B2 = new button(“2”); …B6 = new button(“6”); } Public void display() { f.setLayout(new ridLayout()); f.add(b1); f.add(b2); f.add(b6); f.pack; f.setVisible(true); } Public static void main (String[] args) { Example examp = new Example(); Examp.display(); } Output: will display a window with six buttons arranged in one row 70. MyListener is a listener class of action events: Frame f = new Frame (“Frame”); MenuBar m1 = new MenuBar(); Menu menu1 = new Menu(“File”); Menu1.addActionListener(new MyListener); m1.add(menu1); f.setMenuBar(m1); The code will add an action listener to the File menu. 71. Which of the following layout manager enables you to assign row and column numbers to arrange components? ridLayout 72. Which of the following method enables you to disable the layout manager for a container? container.setLayout(0); 73 A: When you resize a window containing buttons assigned with BorderLayout, the ralative positions of the button changes. False B: When you add a component to an empty window assigned with BorderLayout, the window adds the component to the center region by default. True 74. Consider an example.txt file saved in your working directory that contains the following content: User Name = Admin Password = Administrator Host = localhost Which of the following code will be used to read the content of the example.txt and display it on console: Import java.io*; Public class Example( Public static void main(String[] args) { String str; Try{ FileReader fr = new FileReader(“example.txt”); BufferedReader br = new BufferedReader(fr); While ((str = br.readLine()) != null) System.out.println(str);} Catch (IO Exception e){System.out.println(e.getMessage())} } 75. Consider the following code: Public class Example{ Public static void main (String args[]) throws InterruptedException { String info[] = { “Java”, “C++” }; For (int i = 0; i < info.length; i++) { Thread.sleep(4000); System.out.println(info[i]); } } Correct option: The code will display Java and C++ 76. Consider the following code: Import.java.io/*; Class Example{ Public static void main (String args[]) { DataInputStream fi = new DataInputStream(System.in); Try{ fi.readChar();} Catch(IOException e) {System.exit(0);} Finally{System.out.println(“Doing finally”);} } The code will compile, execute, wait for a key press, prints “Doing finally” on a key press, and then exit. 77. Consider the following code: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Example( public static void main(String[] args) throw IOException{ FileInputStream in = null; FileOutputStream out = null; try{ in = new FileInputStream(“OutputFile.txt”); out = new FileOutputStream(“InputFile.txt”); int c; while((c = in.read()) !=-1){ out.write(c); } } finally{ if (in != null){ out.close();} if (out != null){ out.close(); } The code will copy the content of the OutputFile.txt file to the InputFile.txt file. 78. Which of the following is NOT an valid state of a thread? Listening 79. Which of the code can be used in order to start a server for communication? Answer: public server () { try { ServerSocket s1 = new ServerSocket(1001); } catch(IOException e) { fail(e,"server can’t be started"); } System.out.println("Server Started"); this.start(); } 80. Which of the following is the property of an algorithm? Output 81.Henry is assigned a task to write a code for sorting a given list of numbers. According to the requirement, the list to be sorted is short. Therefore , he decided to sort the list by using bubble sort. Which of the following algorithm will be use? Answer: 1. Set pass = 1. 2. Repeat step 3 varying j from 0 to n - 1 - pass. 3. If the element at index j is greater than the element at index j + 1, swap the two elements. 4. Increment pass by 1. 5. If pass <= n - 1 go to step 2. 82. Sam is assigned a task to write code to sort a given list of elements by using the selection sort. Which of the following algorithms would enable Sam to accomplish his task? Answer: 1. Repeat steps 2 and 3 varying j from 0 to n - 2 2. Find the index of the minimum value in arr[j] to arr[n - 1] a. Set min_index = j b.Repeat step c varying i from j + 1 to n - 1 c.If arr[i] < arr[min_index]: i.min_index = i 3. Swap arr[j] with arr[min_index] 83. John has been assigned a task to write code to implement a search operation on a list of elements for locating a paticular value. He decides to implement linear search. Identify the correct algortihm that would enable John to accomplish his task. 1. Accept the value to be searched 2. Set i = 0 3. Repeat step 4 until i = n or arr[i] = value to be searched 4. Increment i by 1 5. If i = n: Display "Not Found" Else Display "Found" 84. Henry needs to write code to implement a search operation on a list of elements to locate a particular value. According to the requrement, the list is huge and is already sorted. Identify the algorithm that would enable Henry to accomplish his task. 1. Accept the element to be searched 2. Set lowerbound = 0 3. Set upperbound = n - 1 4. Set mid = (lowerbound + upperbound)/2 5. If arr[mid] = desired element: a. Display "Found" b. o to step 10 6. If desired element < arr[mid]: a. Set upperbound = mid - 1 7. If desired element > arr[mid] a. Set lowerbound = mid + 1 8. If lowerbound <= upperbound: a. o to step 4 9. Display "Not Found" 10. Exit 85. Which of the following holds true for quick sort algorithm? It works by selecting an element from the list called a pivot then partitioning the list into two parts that may not be equal 86. What is merge sort algorithm? Merge sort algorithm is a sorting algorithm that divides the list to be sorted into two sublists of sizes as nearly equal as possible. The two sublists are sorted separately and then combined. 87. Which of the following holds true for binary search algorithm? To apply binary search algorithm, you should ensure that the list to be searched is sorted. 88. How many comparisons are requred to sort an array of 10 elements using selection sort? 45 (hint: # of swap: (n-1); # of comparision: (n-1) +(n-2)… +1) 89. How many comparisions are required to sort an array of 12 elements by using bubble sort? 66 (hint: # of swap: ¼(n 2 ) -1/4(n), # of comparision ½(n 2 ) – ½(n)) 90. Kelly is working on a code that requires her to compute the offset address of a record by applying a hash function on a key value. She applied a hash function in which a part of the numeric key is considered as an offset address of the corresponding record. Which of the following fomulae has Kelly applied to accomplish the task? Trunctation Method 91. Correct Bubble Sort algorithm: 1. Set pass = 1 2. Repeat step 3 varying j from 1 through n – pass 3. If the element at index j -1 is greater than the element at index j, swap the two elements 4. Increment pass by 1 5. If pass <n go to step 2 92. John is required to add various product IDs in an already existing list containing suck IDs. To avoid duplicate entries, he first need s to ascertain that the ID being entered does not already exist in the list. If the list already contains the product ID, a message should be displayed informing him the same. He has written the following algorithm to search whether a given ID exists in the list or not: 1. Read the Product ID to be searched 2. Set i=0; 3.Repeat step 4 until I = n OR arr[i] = Product ID, where n is the size of the list. 4. Increment I by 1 5. If i = n; display “Not Found” else display “Found” The program will produce the desired result 93. Robin has implemented the following algorithm to develop a program that searches for an element in a list: 1. Accept the element to be searched 2. Set lowerbound = 0 3. Set upperbound = n-1 4. Set mid = (lowerbound + upperbound) / 2 5. if arr[mid] = desired element: a. Display “Found” b. go to step 10 6. If desired element < arr[mid] a. Set upperbound = mid – 2 7. If sesired element > arr[mid] a. Set upperbound = mid + 1 8. If lowerbound <= upperbound a. Go to step 4 9. Display “Not Found” 10. Exit The Program is not executing as desired Step 6(a) should be "Set upperbound=mid-1" 94. Which of the following sorting techniques sorts the list efficiently, if the list to be