Summary

This document contains a set of programming questions, suitable for a final exam. The questions cover fundamental programming concepts in Java, including data types, operators, method overloading, and control flow. No specific exam board could be determined from the provided text.

Full Transcript

Quizlet-style Flashcards: Complete Set of 40 Questions ====================================================== Question 1 ---------- What is the equivalent code for \`if (gender == 1) { if (age \>= 65) { ++seniorFemales; } }\`? - a\. \`if (gender == 1 \|\| age \>= 65) { ++seniorFemales; }\` -...

Quizlet-style Flashcards: Complete Set of 40 Questions ====================================================== Question 1 ---------- What is the equivalent code for \`if (gender == 1) { if (age \>= 65) { ++seniorFemales; } }\`? - a\. \`if (gender == 1 \|\| age \>= 65) { ++seniorFemales; }\` - b\. \`if (gender == 1 && age \>= 65) { ++seniorFemales; }\` - c\. \`if (gender == 1 AND age \>= 65) { ++seniorFemales; }\` - d\. \`if (gender == 1 OR age \>= 65) { ++seniorFemales; }\` Correct Answer: b Question 2 ---------- Which methods are overloaded among the given examples? - Methods listed: - \- public int max(int a, int b) { } - \- public double max(double a, double b) { } - \- public int max(int a, int b, int c) { } - \- public double max(double a, double b, double c) { } - a\. A and B are overloaded; C and D are overloaded. - b\. A and C are overloaded; B and D are overloaded. - c\. A, B, and C are overloaded. - d\. All four methods are overloaded. Correct Answer: d Question 3 ---------- What is the correct code for calling the method \`public void displayValue(int value)\`? - a\. \`int x = 7; void displayValue(x);\` - b\. \`int x = 7; displayValue(x);\` - c\. \`int x = 7; displayValue(int x);\` - d\. \`int x = 7; displayValue(x)\` Correct Answer: b Question 4 ---------- What is the value of \`c\` at the end of the code \`int c = 8; c++; ++c; c %= 5;\`? - a\. \`0\` - b\. \`1\` - c\. \`3\` - d\. None of the above. Correct Answer: a Question 5 ---------- What are the values of \`x\` and \`y\` after executing the code \`int x = 25, y = 8; x += y++;\`? - a\. \`x = 34, y = 9\` - b\. \`x = 25, y = 8\` - c\. \`x = 33, y = 8\` - d\. \`x = 33, y = 9\` Correct Answer: d Question 6 ---------- What is the value of \`result\` after executing the expression \`result = d % a \* c + a % b + a;\` with \`a=4, b=12, c=37, d=51\`? - a\. \`119\` - b\. \`51\` - c\. \`127\` - d\. \`59\` Correct Answer: a Question 7 ---------- Which keyword indicates the \`main\` method can be called without creating an object? - a\. \`stable\` - b\. \`private\` - c\. \`static\` - d\. \`public\` Correct Answer: c Question 8 ---------- What format specifier is used to output float or double values? - a\. \`%f\` - b\. \`%d\` - c\. \`%fd\` - d\. \`%r\` Correct Answer: a Question 9 ---------- Which statement ensures a variable \`temp\` is between 0 and 100 inclusively? - a\. \`if (temp \>= 0 && temp \ 0 && temp \= 0 \|\| temp \ 0 \|\| temp \< 100)\` Correct Answer: a Question 10 ----------- What is the value of \`charges\` after the code \`charges = time \>= 119 ? rate \* 2 : time / 60.0 \* rate;\` with \`time = 180\` and \`rate = 7.00\`? - a\. \`7.00\` - b\. \`14.00\` - c\. \`21.00\` - d\. \`28.00\` Correct Answer: c Quizlet-style Flashcards: Questions 11-20 ========================================= Question 11 ----------- What is the result of swapping the elements of arrays \`x\[\]\` and \`y\[\]\`? - a\. \`x\[\] = {36, 78, 12, 24}, y\[\] = {23, 55, 83, 19}\` - b\. \`x\[\] = {36, 78, 12, 24} and y\[\] = {36, 78, 12, 24}\` - c\. \`This is a compile error.\` - d\. \`Nothing. This is a runtime error.\` Correct Answer: a Question 12 ----------- What does declaring a field \`static\` mean? - a\. A copy of the field for each method in the class. - b\. A copy of the field in each class object. - c\. Only one copy of the field in memory. - d\. Two copies of the field for each method in the class. Correct Answer: c Question 13 ----------- What happens when calling \`System.out.println(account)\` where \`account\` is a BankAccount object? - a\. A runtime error will occur. - b\. The method will display unreadable binary data on the screen. - c\. The \`BankAccount\` object\'s \`toString\` method will be implicitly called. - d\. A compiler error will occur. Correct Answer: c Question 14 ----------- What happens if the \`this\` variable is not the first statement in a constructor? - a\. A compiler error will result if it is not the first statement of the constructor. - b\. A compiler error will result if it is the first statement of the constructor. - c\. Nothing will happen. - d\. The \`this\` variable cannot be used as a constructor call. Correct Answer: a Question 15 ----------- When is polymorphism handled? - a\. Execution - b\. Compilation - c\. Programming - d\. Debugging Correct Answer: a Question 16 ----------- What does the binary search algorithm do? - a\. Is less efficient than the sequential search algorithm. - b\. Will cut the portion of the array being searched in half each time it fails to locate the search value. - c\. Will have a maximum number of comparisons equal to the number of elements in the array. - d\. Will normally have the number of comparisons that is half the number of elements in the array. Correct Answer: b Question 17 ----------- Which of the following is a valid declaration for a ragged array with five rows but no columns? - a\. \`int\[\]\[\] ragged = new int\[5\];\` - b\. \`int\[\]\[\] ragged = new int\[\]\[5\];\` - c\. \`int\[\]\[\] ragged = new int\[5\]\[\];\` - d\. \`int\[\] ragged = new int\[5\];\` Correct Answer: c Question 18 ----------- What will happen when executing the given code with array initialization? - a\. A runtime error will occur. - b\. All the values in the array will be initialized to 10.0. - c\. All the values in the array except the first will be set to 10.0. - d\. The code contains a syntax error and will not compile. Correct Answer: d Question 19 ----------- What will the value variable contain after the given code is executed? - a\. The average of all the values in the \`numbers\` array. - b\. The sum of all the values in the \`numbers\` array. - c\. The lowest value in the \`numbers\` array. - d\. The highest value in the \`numbers\` array. Correct Answer: c Question 20 ----------- Which of the following for-loops is valid given \`String\[\] names = {\"abc\", \"def\", \"ghi\", \"jkl\"};\`? - a\. \`System.out.println(names\[i\].length);\` - b\. \`System.out.println(names\[i\].length);\` - c\. \`System.out.println(names\[i\].length());\` - d\. \`System.out.println(names\[i\].length());\` Correct Answer: c Quizlet-style Flashcards: Questions 21-30 ========================================= Question 21 ----------- The code that might throw an exception must be enclosed in a \_\_\_\_. - a\. throws block - b\. catch block - c\. try block - d\. finally block Correct Answer: c Question 22 ----------- What is \`e\` in the catch block below? - a\. The type of the exception being caught. - b\. The name of the catch block\'s exception parameter. - c\. A finally block. - d\. An exception handler. Correct Answer: b Question 23 ----------- What is an uncaught exception? - a\. A possible exception that never occurs during execution. - b\. An exception that occurs for which the matching catch clause is empty. - c\. An exception that occurs for which there are no matching catch clauses. - d\. Another term for a thrown exception. Correct Answer: c Question 24 ----------- What does the \`throws\` clause of a method specify? - a\. Specifies the exceptions a method catches. - b\. Specifies the exceptions thrown by the calling method. - c\. Specifies the exceptions a method throws. - d\. Specifies the exceptions a method throws and catches. Correct Answer: c Question 25 ----------- Which of the following exceptions is a checked exception? - a\. ArithmeticException - b\. IOException - c\. RuntimeException - d\. InputMismatchException Correct Answer: b Question 26 ----------- Which of the following statements is true about the \`throw\` statement? - a\. The \`throw\` statement is used to throw an exception. - b\. The \`throw\` statement is used to specify that a method will throw an exception. - c\. The \`throw\` statement is used to access an exception parameter. - d\. All of the above. Correct Answer: a Question 27 ----------- Which of the following classes is not used for file input? - a\. FileInputStream - b\. FileReader - c\. ObjectInputStream - d\. Formatter Correct Answer: d Question 28 ----------- What do \`setIn\`, \`setOut\`, and \`setErr\` methods do? - a\. They output data to the standard input, output, and error streams. - b\. They provide the only way to access the standard input, output, and error streams. - c\. They redirect the standard input, output, and error streams. - d\. They empty the standard input, output, and error streams. Correct Answer: c Question 29 ----------- A(n) \_\_ path starts from the directory in which the application began executing. - a\. Absolute - b\. Relative - c\. Parallel - d\. Comparative Correct Answer: b Question 30 ----------- The static method \`isDirectory\` receives a Path and returns \_\_\_\_. - a\. A boolean indicating if the path is a directory. - b\. A boolean indicating whether the file exists. - c\. A boolean indicating if the file is executable. - d\. A boolean indicating if the file is readable. Correct Answer: a Quizlet-style Flashcards: Questions 31-40 ========================================= Question 31 ----------- What does \`Scanner scanner = new Scanner(Paths.get(\"test.txt\"));\` do? - a\. Opens a binary file for input. - b\. Opens a binary file for output. - c\. Opens a text file for input. - d\. Opens a text file for output. Correct Answer: c Question 32 ----------- What is a serialized object? - a\. An object represented by XML that includes the object\'s data. - b\. An object in memory that has been recreated from data in a file. - c\. A standard output stream object used to convert objects in code to data in a file. - d\. None of the above. Correct Answer: b Question 33 ----------- Which method serializes objects as XML in JAXB? - a\. \`toXML\` - b\. \`serialize\` - c\. \`marshal\` - d\. None of the above. Correct Answer: c Question 34 ----------- Which method displays a dialog box to gather input? - a\. \`showMessageDialog\` - b\. \`getInput\` - c\. \`inputDialog\` - d\. \`showInputDialog\` Correct Answer: d Question 35 ----------- What provides the basic attributes and behaviors of a window? - a\. \`JLabel\` - b\. \`JFrame\` - c\. \`JSwing\` - d\. \`JWindowControl\` Correct Answer: b Question 36 ----------- Which method adds a \`JLabel\` to a \`JFrame\`? - a\. \`attach\` - b\. \`contain\` - c\. \`append\` - d\. \`add\` Correct Answer: d Question 37 ----------- Which \`JFrame\` constant indicates that the program should terminate when the window is closed by the user? - a\. \`TERMINATE\_ON\_CLOSE\` - b\. \`IMMEDIATELY\_CLOSE\` - c\. \`EXIT\_ON\_CLOSE\` - d\. All of the above Correct Answer: c Question 38 ----------- Which of the following most completely describes the steps for setting up event handling for a GUI component? - a\. Create a class that represents the event handler, attach the \`JFrame\` to a \`JWindow\` object and register the event handler. - b\. Implement an appropriate event-listener interface and register the event handler. - c\. Create a class that represents the event handler and implement an appropriate event-listener interface. - d\. Create a class that represents the event handler, implement an appropriate event-listener interface, and register the event handler. Correct Answer: d Question 39 ----------- When the user presses Enter in a \`JPasswordField\`, the GUI component generates an \_\_\_\_, which is processed by an object that implements the interface \_\_\_\_. - a\. \`ActionEvent\`, \`ActionListener\` - b\. \`ActionEvent\`, \`ActionEventListener\` - c\. \`TextEvent\`, \`TextListener\` - d\. \`TextEvent\`, \`TextEventListener\` Correct Answer: a Question 40 ----------- A \`JRadioButton\` is different from a \`JCheckBox\` in that \_\_\_\_. - a\. A \`JRadioButton\` is a subclass of \`JToggleButton\`, \`JCheckBox\` is not. - b\. Normally several \`JRadioButtons\` are grouped together and are mutually exclusive. - c\. A \`JRadioButton\` is a type of button, \`JCheckBox\` is not. - d\. A \`JRadioButton\` is a state button, \`JCheckBox\` is not. Correct Answer: b

Use Quizgecko on...
Browser
Browser