Docsity

PrepIQ 190 Introduction to Computer Programming using Java Ultimate Exam, Exams of Technology

Toggle sidebar
bg1
PrepIQ 190 Introduction to
Computer Programming using
Java Ultimate Exam
Question 1. **Which of the following is the correct way to declare an integer
variable named count in Java?**
A) int count;
B) integer count;
C) int count = ;
D) var count;
Answer: A
Explanation: The primitive type for whole numbers in Java is `int`, and the syntax
`int count;` declares a variable without initializing it.
---
Question 2. **What will be the output of the following code?**
```java
int a = 5;
int b = ++a;
System.out.println(b);
```
A) 5
B) 6
C) 7
D) Compilation error
Answer: B
Explanation: The prefix increment operator `++a` increments `a` before its value is
assigned to `b`, so `b` becomes 6.
---
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49

Partial preview of the text

Download PrepIQ 190 Introduction to Computer Programming using Java Ultimate Exam and more Exams Technology in PDF only on Docsity!

Computer Programming using

Java Ultimate Exam

Question 1. Which of the following is the correct way to declare an integer variable named count in Java? A) int count; B) integer count; C) int count = ; D) var count; Answer: A Explanation: The primitive type for whole numbers in Java is int, and the syntax int count; declares a variable without initializing it.

Question 2. What will be the output of the following code?

int a = 5; int b = ++a; System.out.println(b);

A) 5 B) 6 C) 7 D) Compilation error Answer: B Explanation: The prefix increment operator ++a increments a before its value is assigned to b, so b becomes 6.

Computer Programming using

Java Ultimate Exam

Question 3. Which keyword is used to create a subclass that inherits from a superclass? A) implements B) extends C) inherits D) super Answer: B Explanation: In Java, the extends keyword establishes an inheritance relationship between a subclass and its superclass.

Question 4. Which of the following statements about Java’s final keyword is FALSE? A) A final variable cannot be reassigned after initialization. B) A final method cannot be overridden in a subclass. C) A final class cannot be subclassed. D) A final method can be overloaded in the same class. Answer: D Explanation: Overloading a final method is allowed because overloads are separate methods; the statement is true, not false. The false statement is D because final does not prevent overloading; however the question asks which is FALSE, and D is actually true, making it not false. The correct false statement is D because the premise “cannot be overloaded” is false. Therefore D is the correct answer.

Question 5. What is the result of evaluating the expression 7 % 3 in Java? A) 1 B) 2

Computer Programming using

Java Ultimate Exam

Question 8. What is the output of the following loop?

for (int i = 0; i < 3; i++) { if (i == 1) continue; System.out.print(i); }

A) 012 B) 02 C) 01 D) 0 Answer: B Explanation: When i equals 1, continue skips the print statement for that iteration, so only 0 and 2 are printed.

Question 9. Which method signature correctly overrides the toString method from java.lang.Object? A) public String toString() B) public void toString() C) protected String toString(Object o) D) public static String toString() Answer: A

Computer Programming using

Java Ultimate Exam

Explanation: The toString method in Object is public String toString(). Matching the exact signature and return type enables overriding.

Question 10. In Java, which of the following statements about the static keyword is TRUE? A) A static method can access instance variables directly. B) Static members belong to the class, not to any specific object. C) A static class can be instantiated. D) Static methods must be abstract. Answer: B Explanation: static members are associated with the class itself; they exist independently of any object instances.

Question 11. Which of the following is NOT a valid Java identifier? A) _total B) $value C) 2ndPlace D) maxScore Answer: C Explanation: Identifiers cannot begin with a digit; 2ndPlace is illegal.

Question 12. What does the break statement do inside a switch block?

Computer Programming using

Java Ultimate Exam

A) true B) false C) Compilation error D) Runtime exception Answer: B Explanation: == compares object references. s1 refers to the interned string literal, while s2 refers to a distinct object created with new.

Question 15. Which exception is thrown when an array index is accessed outside its bounds? A) NullPointerException B) ArrayIndexOutOfBoundsException C) IllegalArgumentException D) IndexOutOfBoundsException Answer: B Explanation: Accessing an invalid array index triggers ArrayIndexOutOfBoundsException.

Question 16. What is the purpose of the finalize() method in Java? A) To release system resources before garbage collection. B) To initialize static variables. C) To perform cleanup after an object is garbage collected. D) To guarantee deterministic destruction of objects. Answer: C

Computer Programming using

Java Ultimate Exam

Explanation: finalize() is called by the garbage collector before reclaiming an object’s memory, allowing final cleanup. (Note: its use is deprecated in newer Java versions.)

Question 17. Which of the following statements about Java interfaces is FALSE? A) An interface can contain abstract methods only (prior to Java 8). B) A class can implement multiple interfaces. C) Interfaces can have instance fields. D) All methods in an interface are implicitly public. Answer: C Explanation: Interfaces cannot declare instance fields; they may only have constants (public static final).

Question 18. What is the output of the following code snippet?

int x = 10; int y = 20; System.out.println(x > y? x : y);

A) 10 B) 20 C) true D) false Answer: B

Computer Programming using

Java Ultimate Exam

Question 21. What does the java command do when executed with a class name that contains a main method? A) Compiles the source file. B) Executes the compiled bytecode of the specified class. C) Packages the class into a JAR file. D) Generates Javadoc documentation. Answer: B Explanation: java ClassName launches the JVM to run the main method of the compiled class.

Question 22. In Java, which collection guarantees insertion order and does not allow duplicate elements? A) HashSet B) LinkedHashSet C) TreeSet D) ArrayList Answer: B Explanation: LinkedHashSet maintains insertion order while enforcing uniqueness.

Question 23. **Which of the following statements about method overloading is true? ** A) Overloaded methods must have different return types only. B) Overloaded methods must differ in parameter list (type, number, or order). C) Overloaded methods cannot coexist in the same class. D) Overloading is resolved at runtime.

Computer Programming using

Java Ultimate Exam

Answer: B Explanation: Overloading occurs when methods share a name but have distinct parameter signatures; return type alone is insufficient.

Question 24. What will be the result of the following code?

int[] nums = {1, 2, 3, 4, 5}; int sum = 0; for (int n : nums) { sum += n; } System.out.println(sum);

A) 10 B) 15 C) 5 D) 0 Answer: B Explanation: The enhanced for-loop adds each element to sum, resulting in 1+2+3+4+5 = 15.

Question 25. Which of the following is a functional interface in the java.util.function package? A) Predicate

Computer Programming using

Java Ultimate Exam

Explanation: The syntax new InterfaceName() { ... } defines an anonymous class that implements the interface.

Question 28. What will be printed by the following code?

String s = "abc"; s = s.concat("def"); System.out.println(s);

A) abcdef B) abc C) def D) abc+def Answer: A Explanation: concat returns a new string that is the original plus the argument; the reference s is updated to "abcdef".

Question 29. Which of the following statements about Java enums is correct? A) Enums can extend other classes. B) Enums are implicitly final. C) Enums can be instantiated with new. D) Enums cannot have methods. Answer: B

Computer Programming using

Java Ultimate Exam

Explanation: Enum types are implicitly final; they cannot be subclassed. They can have methods, but they cannot extend other classes or be instantiated with new.

Question 30. What is the purpose of the assert keyword in Java? A) To handle checked exceptions. B) To provide a way to test assumptions during development. C) To enforce access control. D) To create immutable objects. Answer: B Explanation: assert statements evaluate a boolean expression and throw an AssertionError if false, aiding debugging.

Question 31. Which of the following statements about Java’s garbage collector is FALSE? A) It automatically reclaims memory occupied by unreachable objects. B) It can be explicitly invoked with System.gc(). C) It guarantees immediate reclamation of memory. D) It runs as a low-priority background thread. Answer: C Explanation: The garbage collector does not guarantee when (or even if) memory will be reclaimed; it is nondeterministic.

Question 32. What is the output of the following code?

Computer Programming using

Java Ultimate Exam

Question 34. What does the @Override annotation indicate? A) The method is overloaded. B) The method overrides a superclass method. C) The method is final. D) The method is abstract. Answer: B Explanation: @Override tells the compiler that the method is intended to override a method from a superclass or interface, and it generates a compile-time error if it does not.

Question 35. Which of the following is a correct way to read a line of text from the console using java.util.Scanner? A) Scanner sc = new Scanner(System.in); String line = sc.readLine(); B) Scanner sc = new Scanner(System.in); String line = sc.nextLine(); C) Scanner sc = new Scanner(); String line = sc.next(); D) Scanner sc = new Scanner(System.in); String line = sc.get(); Answer: B Explanation: nextLine() reads the remainder of the current line, including spaces, from the input source.

Question 36. What is the result of executing the following code?

int[] a = {1,2,3}; int[] b = a; ## Computer Programming using ## Java Ultimate Exam b[0] = 99; System.out.println(a[0]);

A) 1 B) 99 C) Compilation error D) Runtime exception Answer: B Explanation: b and a reference the same array object; modifying b[0] also changes a[0].

Question 37. Which of the following statements about StringBuilder is true? A) It is immutable. B) It is thread-safe. C) It provides efficient mutable string manipulation. D) It cannot be converted to a String. Answer: C Explanation: StringBuilder is mutable and not synchronized, making it efficient for frequent modifications.

Question 38. Which of these is the correct way to declare a generic class that holds elements of any type T? A) class Box { private T value; } B) class Box { private T[] value; }

Computer Programming using

Java Ultimate Exam

C) Duration can be used to represent a date without a time component. D) The API is immutable and thread-safe. Answer: C Explanation: Duration represents a time-based amount (seconds, nanoseconds) and not a date without time; Period is used for date-based amounts.

Question 41. What does the volatile keyword guarantee for a variable? A) The variable cannot be changed after initialization. B) Reads and writes are atomic for all data types. C) Visibility of changes across threads without caching. D) Automatic synchronization of all methods that use the variable. Answer: C Explanation: volatile ensures that reads/writes go directly to main memory, making updates visible to other threads.

Question 42. Which of the following statements correctly creates a thread by extending Thread? A) class MyThread extends Thread { public void run() {} } B) class MyThread implements Thread { public void run() {} } C) Thread t = new Thread() { public void run() {} }; D) Both A and C Answer: D Explanation: Extending Thread (A) and creating an anonymous subclass of Thread (C) both produce a Thread object with overridden run.

Computer Programming using

Java Ultimate Exam

Question 43. What will be the output of the following code?

int x = 5; System.out.println(x++ + ++x);

A) 11 B) 12 C) 13 D) Compilation error Answer: B Explanation: Evaluation order: x++ yields 5 (then x becomes 6); ++x increments to 7 and yields 7; sum = 5 + 7 = 12.

Question 44. Which of the following is a correct way to handle multiple resources using try-with-resources? A) try (BufferedReader br = new BufferedReader(...)) { /.../ } B) try { BufferedReader br = new BufferedReader(...); /.../ } finally { br.close(); } C) try (BufferedReader br = new BufferedReader(...); Scanner sc = new Scanner(...)) { /.../ } D) Both A and C Answer: D Explanation: Both single-resource and multiple-resource try-with-resources syntax are valid.