Introduction to Programming — Final
Exam With questions and answers
with rationales
COURSE TOPICS COVERED
Topic Description
Variables, data types, operators,
Java Fundamentals
input/output, type conversion
Conditional statements (if, else, switch),
Control Flow
loops (for, while, do-while)
Method declaration, parameters, return
Methods
types, scope, overloading
Declaration, initialization, traversal,
Arrays
searching, sorting, multidimensional array
,Topic Description
Object-Oriented Classes, objects, constructors,
Programming encapsulation, inheritance, polymorphism
Strings String methods, immutability, comparison
Linear search, binary search, selection sort
Algorithms
insertion sort
Base cases, recursive cases, recursive
Recursion
problem-solving
Number systems, compilers, pair
Computing Breadth
programming, ethics (if covered)
PART I: JAVA FUNDAMENTALS (Questions 1-30)
1. Which of the following is a valid Java identifier?
• A) 2ndPlace
• B) _myVar
, • C) my-var
• D) class
• E) my var
Answer: B
Rationale: Identifiers must start with a letter, underscore
(_), or dollar sign ($), followed by letters, digits,
underscores, or dollar signs. _myVar is
valid. 2ndPlace starts with a digit, my-var contains a
hyphen, class is a keyword, and my var contains a space.
2. Which of the following is a primitive data type in Java?
• A) String
• B) Integer
• C) boolean
• D) Double
• E) ArrayList
Answer: C
Rationale: Java has eight primitive data
types: byte, short, int, long, float, double, char,
, and boolean. String, Integer, Double, and ArrayList are
reference types.
3. What is the output of the following code?
java
int x = 5;
int y = 2;
System.out.println(x / y);
• A) 2
• B) 2.5
• C) 3
• D) 2.0
• E) Compiler error
Answer: A
Rationale: Integer division truncates the decimal
portion. = 2 (remainder 1 is discarded).
4. What is the output of the following code?