Evil Pop Quizzes for CS-1

  1. write the outline for an empty class (no main())
  2. write a println() statement involving variables
  3. write a println() statement containing escape sequences
  4. do a simple conversion (ft to inches, kilometers to miles, etc) using variables
  5. translate a math formula to Java
  6. store a random number into a variable
  7. write an if statement that is true ¼ of the time (using Math.random)
  8. write a valid function call with literal values or variables

Canonical Solutions

Empty Class

// write the outline for an empty class (no main())
public class Testing
{


}

Println

// write a println() statement
System.out.println( "Here are " + count + " words." );

Println with Escapes

// write a println() statement containing escape sequences
System.out.println( "\t_X_\n\tXXX\n\t_X_\n\t_X_\n" );

Simple Formula/Conversion

// do a simple conversion using variables and numbers
double miles = feet / 5280;

double kelvin = celsius + 273.15;

Harder Formula/Conversion

// translate a math formula to Java
x = (-b + Math.sqrt(b*b - 4*a*c)) / (2*a);

Random Number

// store a random number into a variable
x = 1 + (int)(Math.random()*10);

Random If Statement

// write an if statement that is true 1/4 of the time using Math.random
if ( Math.random() < 0.25 )
{
}

Function Call

// write a valid function call with literal values or variables
drawFace(100, 100, wid, hite);



©2013–2015 Graham Mitchell

This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Creative Commons License