Review Q & A-4

docx

School

University of North Alabama *

*We aren’t endorsed by this school

Course

135

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

13

Uploaded by DukeStarChimpanzee47

Report
Exam 2 – Review Q’s
2 Methods,Classes, Arrays, Collections, ArrayLists I. True or False (2 points each) 1. __T__ Methods can be used to break a complex problem into small manageable pieces. 2. __T__A void method executes a group of statements and then terminates. 3. __T__ A value returning method returns a value to the statement that called it. 4. _F__ A method call terminates with a parenthesis. 5. _ _F__Array is a data structure that stores a different size sequential collection of elements of the same types. 6. ___ If a variable does not reference to an array, the value of the variable is null. 7. __T__ You can create an array by using the new operator. 8. ___T____ The size of an array can be changed after the array is created. 9. ___ F ___ The size of an arraylist can be changed after the array is created. 10. __ _T___ If an array, myList holds ten double values and the indices from 0 to 9. The element myList[9] represents the last element in the array. 11. ___T___ The foreach loop in Java, enables you to traverse the complete array sequentially without using an index variable. 12. _____ Accessing an array out of bound is a common programming error. To avoid it, make sure that you don’t use an index beyond arrayRefVar.length-1.
3 II. Multiple choice (2 points each) 9. This type of method does not return a value. a. null b. void c. empty d. zero 10. Method signature consists of: a. Method name b. Parameter list c. Both a and b d. Only a 11. The output of displayValue method is: int x = 20; displayValue(x); …. // display value public static void displayValue(int n) { System.out.println(“The value is: ” + n); }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4 a. The value is: 20 b. The value is n c. 20 d. An error will occur 12. Two or more methods in a class may have the same name, as long as this is different. a. Their return values b. Their access specifier c. Their parameter lists d. Their memory address 13. Assume that the following method has been defined. public static int mystery (int x, int y) { return x * y; } What value will be stored in the variable result by the following statement that calls the mystery method? int result = mystery(5, 2); a. 10 b. 7 c. 52 d. none of these 14. Using the same method above, what will be stored in the variable result by the following statement that calls the mystery method? int result = mystery(2, 2); a. 10 b. 7 c. 52 d. none of these
5 15. Overloaded methods consist of: a. two or more methods having the same name and the same parameter list. b. two or more methods having the same name. c. two or more methods having the same name but with different parameter list. d. none of the above. 16. When a method named max , is invoked, the flow of control a. transfers to the max method. b. remains in method main c. is in the class d. shows no change
6 17. Code can be reused because of this. methods 18. Combination of the method name and the parameter list. Method signature IV: Short answer (5 points each) 19. The statement below is a method call made by passing a variable as an argument. There is an error in the code. Rewrite it correctly. displayValue(int x); Answer: displayValue (x); 20. A compile error will occur for the showSum method defined below. Rewrite the corrected method header (one error). public static void showSum(double num1, num2) Answer : public static void showSum(double num1, double num2) 21. // Main method int total, value1 = 10, value2 = 5, number1 = 15; double avg = 0.0; total = sum(value1, value2); System.out.println(“The sum of ” + value1+ “and ”+value2 +“is ” +total); …. …. public static int sum(int num1, int num2) { int result = num1 + num2; return result; } What will be the output?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
7 Answer : The sum of 10 and 5 is 22. Find the error(s) in the code below. (5 points) public class FindMax { public static void main(String[] args) { System.out.println(max()); }   public static double max(int num1, int num2) ;{ if (num1 > num2) return num1; else return num2 ; } } 23. public class FindMin { public static void main(String[] args) { System.out.println(min(200.34, 100)); } public static double min(int num1, int num2); { if (num1 < num2) return num1; else return num2; } } 24. public static void nPrintln(String message, int n){ for (int i = 0; i < n; i++) System.out.println(message); } a) Briefly state what the method, nPrintln does
8 Prints a message n number of times. b) Can you invoke the nPrintln method using: nPrintln(“Computer Science”, 2); What is the output? : You are asked to write a program to display a Calculator program by making use of methods. The calculator must be able to perform the following operations. Addition, Subtraction, Multiplication, and Division. The user should also be able to exit the program whenever he/she decides. Briefly describe the steps needed in order to complete your project. Your answer can be in the form of pseudo-code. NOTE: you can design your program the way you would like. This is just an example solution. Calculator Class Main Method : User input Method call to display a menu Method DisplayMenu: Use a loop (include an exit strategy:: -1 to exit or choice #5 to exit) Menu Option #1 – Addition Method call ( addition() ) Display Value Menu Option #2 – Subtraction Method call (sub()) Display Value Menu Option #3 – Multiplication Method call (mult()) Display Value Menu Option #4 – Division Method call(div()) Display Value Menu Option #5 – Exit Method addition: specify the return type, the number of parameters . Example: public static int addition ( int num1, int num2 ) { int sum = num1+num2;
9 return sum; } Similar methods for the rest of the menu options given above goes here. } // End class 25. A Java class file can be built easily using the UML diagram. 27. a. Are the fields in the Rectangle class private or public? private Fields cannot be accessed by code outside the Rectangle class. Fields can be accessed only by methods that are members of the same class. b. List the methods in the UML given above? c. Write the structure of the code for the method setWidth() and getWidth() using the UML defined above. public void setWidth (double w) { width = w; } public double getWidth () { return width; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
10 28. A constructor that does not accept arguments is known as a no-arg constructor . (True /False) 29. Data hiding is important because classes are typically used as components in large software systems. How does data hiding take place with objects in Java ? 30. public class MaximumValue { public static void main(String[] args) { System.out.println(max(1, 2)); }   public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } } Will the code above give a compile error? If your answer is yes – describe the error. 31. Which of the following is a method having same name as that of its class? a) finalize b) delete c) class d) constructor 32. Which method can be defined only once in a program? a) main method b) finalize method c) static method d) private method 33. Write a method to display three numbers in ascending order. public static void displaySortedNumbers(int num1, int num2, int num3)
11 Recall: that void methods display information from a method as opposed to return type methods. 34. Write a get method to return the radius of a circle. public static int getRadius() { return radius; } 35. Write a set method to set a new radius. public static void setRadius(double newRadius) { radius = newRadius; } 36. Differences between an array and an ArrayList in Java? 37. Using the different methods in order to manipulate an ArrayList. 38. In order to use the ArrayList class in your program, you need to include the ‘import’ directive as shown below: import java.util.ArrayList; OR import java.util.*; 39. What is the output ? import java.util.*; public class Main { public static void main(String args[]) { ArrayList<String> colors = new ArrayList<String>(){{ add("Purple"); add("Yellow"); add("Green"); }}; //print the ArrayList System.out.println("Content of ArrayList:"+colors); } }
12 Result: 40. What is a framework in Java? 41. What is a Collection framework in Java ?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
13