Assignment Overview
In this assignment, you will demonstrate your understanding of Java fundamentals and algorithm complexity analysis. This quiz consists of two parts: Java Basics & OOP, and Big O & Complexity Analysis. You must complete both parts to receive full credit.
Java Basics & OOP (1.1)
Syntax, variables, classes, objects, inheritance, polymorphism, encapsulation, and collections
Big O & Complexity (1.2)
Time complexity, space complexity, asymptotic analysis, best/average/worst cases
Topics Covered
1.1 Java Basics & OOP
- Java syntax, variables, and data types - Primitive types, reference types, type casting, operators
- Classes, objects, and inheritance - Class structure, constructors, super/subclass relationships
- Polymorphism and encapsulation - Method overriding, overloading, access modifiers, getters/setters
- Collections framework - ArrayList, HashMap, HashSet, LinkedList, iterators
1.2 Big O & Complexity Analysis
- Time complexity and space complexity - Measuring algorithm performance
- Big O notation and asymptotic analysis - O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ)
- Best, average, and worst case scenarios - Understanding different input cases
- Analyzing code efficiency - Counting operations, identifying bottlenecks
Part 1: Java Basics & OOP (50 Points)
Answer all questions in this section. For coding questions, write complete, compilable Java code.
Data Types & Variables (10 points)
Answer the following questions about Java data types:
- What is the difference between
intandIntegerin Java? When would you use each? - Explain autoboxing and unboxing with an example.
- What will be the output of the following code? Explain why.
String s1 = "Hello"; String s2 = "Hello"; String s3 = new String("Hello"); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s1.equals(s3));
Classes & Objects (10 points)
Create a class BankAccount with the following requirements:
- Private fields:
accountNumber(String),balance(double),accountHolder(String) - A parameterized constructor that initializes all fields
- Getters for all fields, setter only for
accountHolder - Methods:
deposit(double amount),withdraw(double amount)that returns boolean (false if insufficient funds) - Override
toString()to return account details
Inheritance & Polymorphism (15 points)
Using the BankAccount class from Q2, create:
- A subclass
SavingsAccountthat adds aninterestRatefield and anaddInterest()method - A subclass
CheckingAccountthat adds anoverdraftLimitfield and overrideswithdraw()to allow overdraft up to the limit - Write a
mainmethod that demonstrates polymorphism by creating an array ofBankAccountobjects containing both types of accounts, then calling methods on each
Collections Framework (15 points)
Write Java code to complete the following tasks:
- Create an
ArrayList<String>, add 5 names, remove duplicates using aHashSet, and print the result - Create a
HashMap<String, Integer>to store student names and their scores. Write code to:- Add 5 students with scores
- Find the student with the highest score
- Print all students who scored above average
- Explain when you would use
ArrayListvsLinkedList. Provide a specific use case for each.
Part 2: Big O & Complexity Analysis (50 Points)
For each question, show your work and explain your reasoning. Partial credit will be given for correct methodology.
Identify Time Complexity (15 points)
Determine the Big O time complexity for each of the following code snippets. Explain your reasoning.
a)
public int sumArray(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
b)
public void printPairs(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.println(arr[i] + ", " + arr[j]);
}
}
}
c)
public int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
Complex Analysis (15 points)
Analyze the following code and determine both time and space complexity:
public List<List<Integer>> generateSubsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
for (int num : nums) {
int size = result.size();
for (int i = 0; i < size; i++) {
List<Integer> subset = new ArrayList<>(result.get(i));
subset.add(num);
result.add(subset);
}
}
return result;
}
- What is the time complexity? Explain step by step.
- What is the space complexity? Consider the output as part of the space.
- How many subsets will be generated for an array of size n?
Best, Average, Worst Case (10 points)
For the following linear search algorithm:
public int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
- What is the best case time complexity? When does it occur?
- What is the average case time complexity? Explain your reasoning.
- What is the worst case time complexity? When does it occur?
Compare & Optimize (10 points)
Given two solutions to find if an array contains any duplicates:
Solution A:
public boolean hasDuplicateA(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) return true;
}
}
return false;
}
Solution B:
public boolean hasDuplicateB(int[] arr) {
Set<Integer> seen = new HashSet<>();
for (int num : arr) {
if (seen.contains(num)) return true;
seen.add(num);
}
return false;
}
- What is the time and space complexity of Solution A?
- What is the time and space complexity of Solution B?
- Which solution would you prefer and why? Are there scenarios where you might prefer the other?
Submission
Create a public GitHub repository with the exact name shown below:
Required Repository Name
java-fundamentals-quiz
Required Files
java-fundamentals-quiz/
├── answers.md (or answers.pdf) # Your answers to all questions
├── src/
│ ├── BankAccount.java # Q2 solution
│ ├── SavingsAccount.java # Q3a solution
│ ├── CheckingAccount.java # Q3b solution
│ ├── BankDemo.java # Q3c demonstration
│ └── CollectionsDemo.java # Q4 solutions
└── README.md # REQUIRED - see contents below
README.md Must Include:
- Your full name and submission date
- Brief description of your approach to each part
- Any resources used (documentation, tutorials, etc.)
- Instructions to compile and run your Java files
Do Include
- Complete answers to all 8 questions
- Compilable Java code for programming questions
- Clear explanations for complexity analysis
- Step-by-step reasoning for each answer
- README.md with all required sections
- Well-organized folder structure
Do Not Include
- Any .class files (compiled bytecode)
- IDE-specific folders (.idea, .vscode, etc.)
- Answers copied from the internet without understanding
- Code that doesn't compile
- Incomplete answers
javac *.java in the src folder.
Enter your GitHub username - we'll verify your repository automatically
Grading Rubric
Your assignment will be graded on the following criteria:
| Criteria | Points | Description |
|---|---|---|
| Part 1: Java Basics & OOP | 50 | Q1 (10), Q2 (10), Q3 (15), Q4 (15) - Correct code, proper OOP usage, complete answers |
| Part 2: Complexity Analysis | 50 | Q5 (15), Q6 (15), Q7 (10), Q8 (10) - Correct complexity, clear explanations |
| Total | 100 |
Part 1 Breakdown
- Q1 (10 pts): Data types understanding, autoboxing, String comparison
- Q2 (10 pts): Correct class structure, encapsulation, methods
- Q3 (15 pts): Inheritance, method overriding, polymorphism demo
- Q4 (15 pts): Collections usage, iteration, comparison
Part 2 Breakdown
- Q5 (15 pts): Correct Big O for all 3 snippets with explanation
- Q6 (15 pts): Time/space analysis, subset count formula
- Q7 (10 pts): Best/average/worst case understanding
- Q8 (10 pts): Comparison, trade-offs, recommendations
Ready to Submit?
Make sure you have completed all requirements and reviewed the grading rubric above.
Submit Your AssignmentWhat You Will Practice
Core Java Concepts (1.1)
Data types, operators, String handling, and the fundamentals of Java programming language
OOP Principles
Encapsulation, inheritance, polymorphism, and designing reusable class hierarchies
Collections Framework
Using ArrayList, HashMap, HashSet, LinkedList, and understanding when to use each
Algorithm Analysis (1.2)
Analyzing time and space complexity, understanding Big O notation, and optimizing code
Pro Tips
Java Best Practices
- Use meaningful variable and method names
- Follow Java naming conventions (camelCase)
- Always use access modifiers appropriately
- Override
toString()for debugging
Complexity Analysis Tips
- Count the number of operations relative to input size
- Look for nested loops - they often indicate O(n²)
- Halving problems usually mean O(log n)
- Consider both time AND space complexity
Study Resources
- Review Java documentation for Collections
- Practice tracing code by hand
- Use Big O cheat sheets for reference
- Test your code with different inputs
Common Mistakes
- Forgetting to consider space complexity
- Confusing == with .equals() for objects
- Not handling edge cases in analysis
- Ignoring constant factors in Big O (they don't matter!)