D JAVA Third Quarter Java Programming Language Edited 1
D JAVA Third Quarter Java Programming Language Edited 1
Department of Education
NATIONAL CAPITAL REGION
SCHOOLS DIVISION OFFICE OF QUEZON CITY
DIRECTIONS: Read the following carefully. Choose and write the letter of the best answer on your
answer sheet.
1. import java.util.*;
2.
3. public class Weekday{
4. public static void main(String[] args){
5. int day;
6. Scanner scan=new Scanner (System.in);
7. System.out.print("Enter Number: ");
8. day=scan.nextInt();
9. switch (day)
10. {
11. case 1: System.out.println("Monday");
12. case 2: System.out.println("Tuesday");
13. case 3: System.out.println("Wednesday");
14. case 4: System.out.println("Thursday");
15. case 5: System.out.println("Friday");
16. case 6: System.out.println("Saturday");
17. case 7: System.out.println("Sunday");
18. }
19. }
20. }
Sample Code 1
3. Winona tries to run the program and it shows too many outputs in each available case statement.
What is missing in line 11 through 17 to correct the logic error?
A. break; B. case C. default D. exit
5. After examining the switch-case code in Sample Code1, decide on the filename to use when
saving the file.
A. It will save as weekday.java. C. Weekdays.java is more applicable one.
B. The Weekday.java is the right filename. D. The file will be saved as weekdays.java.
6. What will happen if import statement will be removed?
A. The other part of the program will be missing.
B. Line of the codes will turn into semantic error.
C. There will be a symbol that could not be found in class.
D. Some statements inside the program will turn into syntax error.
7. What is the length of the following array: byte[] data = { 12, 34, 9, 0, -62, 88 }; ?
A. 3 B. 4 C. 5 D. 6
8. In the declaration, int[] ar= {2, 4, 6, 8}; , what will be the legal indices?
A. 0, 1, 2, 3 B. 1, 2, 3, 4 C. 2, 3, 4, 5 D. 4, 5, 6, 7
10. To find the length of the array in item number 9, what will be the statement to use?
A. ar.length(); B. ar.length; c. ar.size(); D. ar.size;
11. Which of the following statement is true about arrays?
A. Arrays can be of any type.
B. Array sizes can change dynamically.
C. Many values of various types can be kept in arrays.
D. Arrays are objects that can store multiple values of the same type.
12. What will the output in this code snippet int[] arr = {1, 2, 3, 4, 5}; System.out.println(arr[4]);?
A. 2 B. 3 C. 4 D. 5
13. How do you access the fourth element in an array with a variable name as myArray?
A. myArray[2] B. myArray[3] C. myArray[4] D. myArray[5]
14. What is the error in the following code fragment? double [] average = new double [20];
average [20] = 15.25; ?
A. A cast is required C. Array Out-of-bounds error
B. Data not initialized D. A two-dimensional array is required
15. The programmer uses two-dimensional array in his program source code that consists of 4 rows
and 3 columns. Which of the following declaration should be used?
A. int myArray [3] [4]; C. int myArray [ ] [ ] =new int [3][4];
B. int myArray [4] [3]; D. int myArray [ ] [ ] =new int [4][3];
16. In declaration of one dimensional array: int [ ] MyArray = { 2, 87, 45, 25, 12};. What would be the
best way of sorting to use to arrange the elements of MyArray [ ] in ascending order?
A. Arrays.sort(MyArray); C. Arrays.sort(MyArray,1,4);
B. Arrays.sort(MyArray,0,4); D. Arrays.sort(MyArray,1,5);
17. What would be the needed java package when using the Arrays.sort() class?
A. import java.io.*; C. import java.util.*;
B. import java.lang.*; D. import javax.swing.*;
18. What is the usage of startExpression in for statement?
A. Decrementation of counter. C. Initialization of counter.
B. Expression to test the limit. D. Increase the value of counter.
22. Amelie assigned her pin as 042521 to log-in her account. Which of the following while loop entry
conditions show correct Boolean expression?
A. while (pin==042521) C. while (pin!=042521);
B. while (pin<=042521) D. while (pin!=042521);
23. You are going to create a multiplication table by 5. You must display multiplicand, multiplier, and
product inside the for loop. How are you going to print the value inside the for loop?
A. System.out.println(“5\n”+num+”\n”+5*ctr); C. System.out.println(“5\t*\t”+num+”\t=\t”+5*ctr);
B. System.out.println(“5\n”+num+”\n”+5*num); D.System.out.println(“5\t*\t”+num+”\t=\t”+5*num);
24. How many increments does the unary operator ++ adds to the value of counter?
A. 1 B. 2 C. 3 D. 4
For item no. 25 – 29. Analyze the Sample Code 2 using for-loop.
1. // Computes the factorial of a number.
2. import java.util.*;
3. public class Factorial{
4. public static void main(String[]args ){
5. Scanner scan=new Scanner (System.in());
6. int count; // Loop index
7. int num; // Number whose factorial is to be computed
8. factorial = 1; // Factorial initialized
9. System.out.print( “ Enter number: “);
10. num=scan.nextInt();
11. for ( _________________________________________ )
12. factorial = factorial * ___________;
13. System.out.println( num + “! is “ + factorial);
14. }
15. }
Sample Code 2
27. How are you going the write the for-loop statement in line 11 of Sample Code 2?
A. for(count=0;count==num;count++) C. for(count=0;count>=num;count++)
B. for(count=1;count<=num;count++) D. for(count=1;count!=num;count++)
28. What variable will be multiplied to factorial to get the factorial value?
A. count B. factorial C. num D. number
29. In line 2, to make the import statement to be specific to scanner, what keyword will be used
instead of *?
A. Scan B. scan C. Scanner D. scanner
32. What kind of inheritance permits a subclass to derive from more than one superclass?
A. double B. hierarchical C. multilevel D. multiple
34. Which of the following claims regarding private members in a superclass is accurate?
A. Inherited by subclasses.
B. Cannot be accessed by subclasses.
C. Can be accessed by subclasses using the dot operator.
D. Can only be accessed by subclasses within the same package.
35. Which keyword is used to access a superclass constructor from a subclass constructor?
A. extends B. implements C. super D. this
36. What keyword in the program allows you to define the block of codes to be tested for errors while
it is being executed?
A. catch B. class C. try D. void
37. It is a keyword used to define a block of codes to be executed if an error occurs in the block found
in question number 36.
A. catch B. main C. static D. try
38. What should be the result of the statement indicated on line 12 of the program source code?
A. Exception Found: SecurityException
B. Exception Found: ArithmeticException
C. Exception Found: FileNotFoundException
D. Exception Found: ArrayIndexOutOfBoundsException
39. What variable could change the value, to avoid the exception been executed?
A. a B. b C. c D. e
40. What does the keyword use to describe the inheritance of java?
A. catch B. extends C. inherit D. throws
41. If import java.io.* is used, what is needed to be thrown inside the main method?
A. Exception C. InfinityException
B. IOException D. DivisionByZero Exception
42. What would be the best way to use if the program accidentally commits a runtime error?
A. throw B. try catch C. throws IOException D. user define exception
45. What did occur through inheritance, when sub classes and method in a superclass is related to
each other?
A. Abstraction B. Encapsulation C. Inheritance D. Polymorphism
46. Maria needs to implement encapsulation in java. How is she going to do this?
A. Make the instance variables private.
B. Instance variable must be set in public.
C. Make the variable available in all part of the program.
D. Variables must be accessed directly inside the program.
47. Jose compiled his program and caught a checked exception. What must he do to correct this
error?
A. Checked exceptions aren’t required to be handled.
B. Exception must be handled by throwing it inside the code.
C. Either by re-throwing or with a try-catch block can help to correct exception.
D. Handling this exception needs to have a proper guidance from the superior.
48. John compiles his program and receives a syntax error. Which of the following best describes the
syntax error?
A. Unable to Run C. Wrong Input
B. Unwanted Results D. Wrong Spelling
49. What will happen if import statement is removed in the program that uses BufferedReader ?
A. The class type Buffered Reader cannot be read.
B. The BufferedReader class type would function smoothly.
C. BufferedReader can function within the scope of main method.
D. BufferedReader can function effectively as well as the IOException.
50. Martina uses BufferedReader in her program source code. She uses a variable with a data type
integer. In parsing the value of the variable, she uses Scan.nextInt. What would be the correct
parse method to use in her program?
A. Interger.parseint(); C. Float.parsefloat();
B. Integer.parseInt(); D. Float.parseFloat();
Prepared by:
Validated by:
Approved by:
1. B 26. B
2. C 27. B
3. A 28. A
4. D 29. C
5. B 30. C
6. C 31. C
7. D 32. D
8. A 33. A
9. D 34. B
10. B 35. C
11. D 36. C
12. D 37. A
13. B 38. B
14. C 39. B
15. D 40. B
16. D 41. D
17. C 42. B
18. C 43. D
19. A 44. C
20. A 45. D
21. D 46. A
22. A 47. C
23. C 48. D
24. A 49. A
25. A 50. B