AP CSA 4.11 Exercise 1: Rows First, Then Columns

Unit 4: Data Collections · Lesson 4.11 · Exercise 1

Rows First, Then Columns

2D Array Creation and Access. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

grid[r][c] is row then column, in that order, always. Swapping them compiles perfectly and gives the wrong answer on every rectangle that is not a square, which is why this exercise never uses one.

What to write

  1. Read two integers, rows and cols, then rows times cols integers filling the grid row by row.
  2. Print the number of rows, then the number of columns, each on its own line.
  3. Print the value in the top left corner, then the bottom right corner.
  4. Print the sum of the first row.
  5. Print the sum of the first column.

Your program reads

Two integers, rows and cols, then rows times cols integers in row order.

Your program prints

Six lines: the row count, the column count, two corners and two sums.

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
2 3
1 2 3
4 5 6
Example 1 output
2
3
1
6
6
5
Example 2 input
1 1
9
Example 2 output
1
1
9
9
9
9

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

grid.length is the number of ROWS. grid[0].length is the number of columns in row 0.

Hint 2

Summing the first row holds the row index at 0 and moves the column. Summing the first column does the opposite.

Hint 3

The bottom right corner is grid[rows - 1][cols - 1]. Both indexes stop one short.

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]