Quick actions

cmd+k|ctrl+k

Navigation

Languages

Week1Mock1MCQ32

Snippet info

Language

Java

Visibility

public

Author

masterhun

Created

2024-04-08T21:02:11.860958Z

Updated

2024-04-08T21:09:16.823Z



// import java.util.*;


public class Main {
    
    public static void main(String[] args) {
        
        // Harbor, Week 1, Mock 1, PR_PracticeTest_1.pdf
        // AP CS A 
        // MCQ 32
 
 
        int nums[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
 
        // I. 
        for (int r = 0; r < nums.length; r++)
	    {
            for (int c = 0; c < nums[0].length; c++)
            {
                System.out.print(nums[r][c] + " ");
		    }
        System.out.print("\n");
        }

        System.out.println();

        // II. 
        for (int[] row: nums)
        {
            for (int col: row)
            {
 			    System.out.print (col + " ");
		    }
        System.out.println("");
	    }
        
        /*
        // III will go out of bounds. 
        // ArrayIndexOutOfBoundsException
        // The r (rows) will iterate as many times as there are columns. 
        // If there are fewer rows than columns, the index will go out of bounds. 
      
        for (int r = 0; r < nums[0].length; r++)
        {
            for (int c = 0; c < nums.length; c++)
            {
                System.out.print (nums[r][c] + " ");
		    }
            System.out.print ("\n");
        }
        */

    }
}
INFO