Code Analysis

Code Analysis is a Division B and Division C that was first run as a trial event at the 2018 Virginia state tournament and at the 2019 Wisconsin state tournament. Competitors are asked to determine the output or error that a given piece of code outputs without running the code. Each team is allowed one 8.5"x11" double-sided reference sheet, or two single-sided reference sheets of the same size.

The Event
Teams are not allowed to bring electronic devices into the testing room. All code given will be in Java, but problems focus on general programming concepts as opposed to specific qualities of Java. Problems may be written in a variety of ways, such as a series of statements, a method to be called with specific arguments, or a complete program with a main method. All inputs and values will be specified, and no external inputs are allowed. All code given will also be properly indented.

While problems may be written in different ways, there are only two types of answers that will be requested. The competitor will determine the output of the code as created by System.out.print, System.out.printIn, and/or System.out.format if a problem has an output. Space will be provided for the competitor to write their answer, and if the answer depends on spacing then a grid will be given so the answer is aligned properly. However, if the code has an error, then the competitor will be asked to describe the run-time error that occurs when the code is run. The error message or exception type does not need to be reproduced, but the competitor must describe what goes wrong and how to correct it.

Subjectivity in Test Writing
Test writers are free to craft the test according to their own coding philosophies. Based on an informal survey of a half dozen 2018/2019 tests:
 * some embrace obfuscation, while others stick to clean ("honest") code.
 * nearly all tests have at least one problem with recursion, although this recursion often ends up being simple single-branch nesting of no more than 3 levels.
 * some tests emphasize mastery of printf and precise output formatting -- \n, whitespace, ASCII, etc. Other tests simply calculate numbers and print them, one per line.
 * non-descriptive variable names and method names are very common -- i, x, n, foo, etc.
 * some tests use nonsensical junk code with zero practical value. Other tests are full of real, usable code -- sorting, towers of hanoi, prime number calculations, etc.

Binary
Binary is the most basic form of data used by a computer. While most programs are not written in binary, it is important to have an understanding of how binary works. There are only two digits used in binary code - 0 and 1. Each number is known as a bit, and there are eight bits in a byte. Half a byte (4 bits) is also known as a nibble. Individual bits can be manipulated by bitwise operations, such as AND (&), OR (|), or XOR (^). Binary numbers are also known as bit strings, and when translated into decimal or hexadecimal numbers encode for specific data.

Operators
Java can manipulate variables in a variety of ways using operators. An operator represents an action or a process, and return the result of that action being performed on one or more operands. The operand is the value which a given action is being performed on. Some operators appear much more frequently than others, but all have their own uses.

Arithmetic Operators
In most cases, arithmetic operators are the same as they are in algebra. However, Java has a couple of additional operators not typically found in math.

Relational Operators
Relational operators show the relationships between two values. All relational operators return a boolean, meaning that they are either true or false. Some of these are also found in algebra, just like the arithmetic operators.

Boolean Operators
Compares two boolean variables and returns a boolean.

De Morgan’s Law
Given that A and B are both boolean variables, De Morgan’s Law states that
 * [math]!(A \& B) == !A \| !B[/math]

and
 * [math]!(A \| B) == !A \& !B[/math]

Short-Circuit Operators
Short circuit operators will not evaluate the second part of a boolean operation if it isn’t necessary. It is written in code as || for OR operations and && for AND operations.

An example, if the condition being evaluated is A || B and A is true, then regardless what B is, A || B will be true, so the computer doesn’t evaluate B. For the expression A && B and A was false, then the expression would return false regardless of what B is.

The reason why a short-circuit for XOR doesn’t exist is because if you evaluate A ^ B, if A is either true or false, the value B still needs to be evaluated to see whether the expression A ^ B returns true or false.

Bitwise Operators
NOTE: All bitwise evaluations are done in binary. They all return the primitive number type (int, long, byte, char) back.

Scoring
Each problem specifies its own worth. No one problem can be worth more than 20% of the total score possible.

Resources

 * Official Java documentation
 * [[Media:C_CodeAnalysis2019_-_OFFICIAL_RULES.pdf|C_CodeAnalysis2019 - OFFICIAL_RULES.pdf]]
 * Wolfram Alpha - De Morgan's Laws