AP CSA 2.11 Exercise 1: Rows and Columns

Unit 2: Selection and Iteration · Lesson 2.11 · Exercise 1

Rows and Columns

Nested Iteration. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

The inner loop finishes completely for every single pass of the outer one. Printing a shape makes that visible, because a misplaced newline turns a rectangle into a column.

What to write

  1. Read two integers, rows and cols.
  2. Print a rectangle of asterisks with that many rows and columns, one row per line.
  3. Print a left aligned triangle where row i has i asterisks, for i from 1 to rows.
  4. Print the number of times the inner loop body ran while drawing the rectangle.
  5. Print the number of times it ran while drawing the triangle.

Your program reads

Two integers on separate lines: rows, then cols. Both are at least 1.

Your program prints

The rectangle, then the triangle, then two counts.

Worked examples

These are the cases you can see. There are more you cannot, and they use different values, so an answer that prints these numbers as constants will fail.

Example 1 input
3
4
Example 1 output
****
****
****
*
**
***
12
6
Example 2 input
1
1
Example 2 output
*
*
1
1

Your answer

Main.java

Input for the Run button

Run sends whatever is in the input box below. Submitting runs your program against every test case, including hidden ones with different input.


  

  

Stuck?

Hint 1

Use System.out.print inside the inner loop and an empty System.out.println() after it, so the line break happens once per row.

Hint 2

The triangle inner loop stops at the OUTER counter, not at cols. That single change is what makes it a triangle.

Hint 3

The rectangle count is always rows times cols. The triangle count is the sum 1 + 2 + ... + rows, which is why it grows more slowly.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]