Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Quiz: Best Seats in the House

DZone's Guide to

Java Quiz: Best Seats in the House

Can you figure out the number of free vs. reserved seats at a small movie theater? You don't want to lose out on your chance to see Batman v Superman, do you?

· Java Zone ·
Free Resource

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend.

Here's the answer to last week's quiz, courtesy of Sar Maroof:

1. Calling the MyAbstract constructor

MyAbstract() // Calling MyAbstruct() no-arg constructor
       x*=2 = 3*2 = 6
       method(3)                  // MySuper class overwrites method one-arg
       method() + 3               // method() no-arg is overwritten in MySub
       return x + 5 = 0 + 5 = 5   // Because x refers to the x in MySuper (Not the x of MyAbstract), which is a subclass of MyAbstract
       method() + 3 = 5 + 3 = 8;
       method(3) returns 8;
       printing the first x = 8;

Printing the first x = 8;

2. Calling the MySuper() constructor

int x = 2
    MySuper() // Calling MySuper() no-arg constructor
      method() // method() no-arg is overwritten in MySub
      return x + 5 = 2 + 5 = 7; // x refers to the x in MySuper, which is = 2
      printing the second x = 7;

Printing the second x = 7;

3. Calling MySub() Constructor

// Calling MySub() no-arg constructor
      x += 3 = 2 + 3 = 5

Printing the third x = 5

The Result is: -x8 – x7 – x5

Here is the next quiz:

The Following Program Determines the Status of Twenty Seats in a Movie Theater:

public class Seat {
     public enum Status {
         FREE, RESERVED, OCCUPIED;
     }
     private Status status;
     private int row = 1;
     private int col = 1;
     public static final int NUMBER_ROWS = 5;
     public static final int NUMBER_COLS = 4;
     public Seat(int row, int col) {
         this.row = row;
         this.col = col;
         this.status = Status.FREE;
     }
     private Seat[][] hall = new Seat[NUMBER_ROWS + 1][NUMBER_COLS + 1];
     public void initHall(){
         Seat seat = new Seat(0,0);
         for(int row = 1; row <= NUMBER_ROWS; row ++ ) {
             for(int col = 1; col <= NUMBER_COLS; col ++ ) {
                 seat = new Seat(row,col);
                 hall[row][col] = seat;
                 if(row <= 4 && col < 3) {
                     seat.status = Status.RESERVED;
                 }
                 if(row >= 3 && col != 3) {
                     seat.status = Status.OCCUPIED;
                 }
             }
         }
     }
     public static void main(String[] args){
         Seat seat = new Seat(0,0);
         seat.initHall();
     }
}


Purpose of the Quiz

  1. This is a practical example to learn some tricks and behaviors of Java multidimensional arrays.
  2. This is a practical example of enumeration.
  3. This will help you imagine how conditional statements change the results, as well as stretch your mind.
  4. This provides an exercise to write a method to support your theory.

This quiz is aimed at intermediate to advanced Java programmers. Have fun! Let us know your thoughts in the comments!   

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend. 

Topics:
java ,quiz ,arrays ,multidimensional arrays ,enumeration

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}