Over a million developers have joined DZone.

Java Quiz: Casting Objects in Java

A new quiz on working with an array of objects in Java.

· Java Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

First, let's look at the answer to last week's quiz.

if(row <= 4 && col < 3)

Status = RESERVED;

The above statement changes some of the seat status from free to reserved as shown below

if(row >= 3 && col != 3)

Status = OCCUPIED;

The above statement changes some of the seat status from (free and reserved) to occupied as shown below

// First Row

hall[1][1] //changed from Free to Res

hall[1][2] //changed from Free to Res


// First Row

hall[1][1] Reserved

hall[1][2] Reserved

hall[1][3] Free

hall[1][4] Free
// Second Row

hall[2][1] //changed from Free to Res

hall[2][2] //changed from Free to Res


// Second Row

hall[2][1] Reserved

hall[2][2] Reserved

hall[2][3] Free

hall[2][4] Free
// Third Row

hall[3][1] //changed from Free to Res

hall[3][2] //changed from Free to Res


// Third Row

hall[3][1] //changed from Res to Occ

hall[3][2] //changed from Res to Occ

hall[3][3] Free

hall[3][4] //changed from Free to Occ
// Fourth Row

hall[4][1] //changed from Free to Res

hall[4][2] //changed from Free to Res


// Fourth Row

hall[4][1] //changed from Res to Occ

hall[4][2] //changed from Res to Occ

hall[4][3] Free

hall[4][4] //changed from Free to Occ
// Fifth Row




// Fifth Row

hall[5][1] //changed from Free to Occ

hall[5][2] //changed from Free to Occ

hall[5][3] Free

hall[5][4] //changed from Free to Occ

Answer B is correct!

The result is: free = 7, reserved = 4 and occupied = 9

Here's another method that may help clarify the above answer:

public void getSeatStatus(){
int nrFr = 0;
int nrOC = 0;
int nrRS = 0;
for(int row = 1; row <= NUMBER_ROWS; row ++ ) {
for(int col = 1; col <= NUMBER_COLS; col ++ ) {
if(hall[row][col].status == Status.RESERVED) {
nrRS ++ ;
}else if(hall[row][col].status == Status.OCCUPIED) {
nrOC ++ ;
}else {
nrFr ++ ;
System.out.print("nr free: " + nrFr + " nr reserved: " + nrRS + " nr occupied: " + nrOC);
public static void main(String[] args){
Seat seat = new Seat(0,0);

You can find this week's quiz below. This quiz is aimed at intermediate to advanced Java programmers.

Purpose of the Quiz

  1. Learn some tricks and behaviors about casting objects.

  2. Find out whether it is possible to cast an object and add it with the object to a single Java ArrayList.

  3. Imagine the changes of the elements in an ArrayList.

  4. Write a piece of code to check your expectations.

The following program adds 12 objects to a list:

public class MySuper {
   public int id = 20;
    public int getNumber(){
        return 12;

public class MySub extends MySuper {
    int id = 40;
    public int getNumber(){
        return 15;

import java.util.ArrayList;
import java.util.List;

public class MyApp {

  private List<MySuper> listMySub = new ArrayList<MySuper>();

    void method(){
        for(int i = 0; i < 12; i ++ ) {
            MySuper myS = new MySub();
            MySuper mySuper = new MySuper();
            if(i < 6 || i == 10) {
            }else if(i >= 7) {
            }else {
    public static void main(String[] args){
        MyApp ma = new MyApp();
  1. Assuming that the index of the twelve objects starts from 0 to 11, which one of the following statements is true about the sixth and the eighth objects in the ArrayList?

  2. Add a piece of code to this program to check out your answer!

As always, you can find Sar at his site. Let us know your thoughts in the comments!

Building Identity Management, including authentication and authorization? Try Stormpath! Our REST API and robust Java SDK support can eliminate your security risk and can be implemented in minutes. Sign up, and never build auth again!


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}