Assignment 1-A

Java Basics Quiz

Test your understanding of Java fundamentals and complexity analysis concepts. This comprehensive quiz covers Java syntax, OOP principles, collections framework, Big O notation, and algorithm analysis.

2-3 hours
Intermediate
100 Points
Submit Assignment
What You'll Practice
  • Java syntax and data types
  • OOP concepts (inheritance, polymorphism)
  • Collections framework usage
  • Big O notation analysis
  • Algorithm efficiency evaluation
Contents
01

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.

Format: This is a written assignment. Create a PDF or Markdown document with your answers to all questions. Include code snippets where required and show your work for complexity analysis problems.
Skills Applied: This assignment tests your understanding of Java Basics & OOP (Topic 1.1) and Big O & Complexity Analysis (Topic 1.2) from Module 1.
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

Ready to submit? Already completed the assignment? Submit your work now!
Submit Now
02

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
03

Part 1: Java Basics & OOP (50 Points)

Answer all questions in this section. For coding questions, write complete, compilable Java code.

Q1
Data Types & Variables (10 points)

Answer the following questions about Java data types:

  1. What is the difference between int and Integer in Java? When would you use each?
  2. Explain autoboxing and unboxing with an example.
  3. 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));
Q2
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
Q3
Inheritance & Polymorphism (15 points)

Using the BankAccount class from Q2, create:

  1. A subclass SavingsAccount that adds an interestRate field and an addInterest() method
  2. A subclass CheckingAccount that adds an overdraftLimit field and overrides withdraw() to allow overdraft up to the limit
  3. Write a main method that demonstrates polymorphism by creating an array of BankAccount objects containing both types of accounts, then calling methods on each
Q4
Collections Framework (15 points)

Write Java code to complete the following tasks:

  1. Create an ArrayList<String>, add 5 names, remove duplicates using a HashSet, and print the result
  2. 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
  3. Explain when you would use ArrayList vs LinkedList. Provide a specific use case for each.
04

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.

Q5
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;
}
Q6
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;
}
  1. What is the time complexity? Explain step by step.
  2. What is the space complexity? Consider the output as part of the space.
  3. How many subsets will be generated for an array of size n?
Q7
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;
}
  1. What is the best case time complexity? When does it occur?
  2. What is the average case time complexity? Explain your reasoning.
  3. What is the worst case time complexity? When does it occur?
Q8
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;
}
  1. What is the time and space complexity of Solution A?
  2. What is the time and space complexity of Solution B?
  3. Which solution would you prefer and why? Are there scenarios where you might prefer the other?
05

Submission

Create a public GitHub repository with the exact name shown below:

Required Repository Name
java-fundamentals-quiz
github.com/<your-username>/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
Important: Make sure all your Java files compile without errors before submitting. Test your code with javac *.java in the src folder.
Submit Your Assignment

Enter your GitHub username - we'll verify your repository automatically

06

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 Assignment
07

What 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

08

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!)
09

Pre-Submission Checklist

Part 1 Checklist
Part 2 Checklist
Repository Checklist