Name the 8 primitive data types in Java and indicate which type can be added to an int variable without producing an error or a value of type other than int.
What is the minimum number of characters in the expression #3do*
that must be
removed for it to represent a legal name for a variable in Java?
What does the following code print?
public class Main { public static void main(String[] args) { double y = 1; double x = (27 % 7) + (y += (15.6 % 4.1)); System.out.println(x); } }
Convert 1101 to binary.
Write the 8-bit two's complement version of -100
Give an example of a "literal" for each of the following types: int
, double
, float
, boolean
, and char
What java keyword is used to indicate a constant value?
Convert the hexadecimal number 110101011011111000011100111
to hexadecimal.
Write the truth table for (P && Q) ^ (! R)
What does the following code print?
public class Main { public static void main(String[] args) { int x = 1; int y = 2; int z = 3; System.out.println( z / y == x ? x : y); System.out.println( 1.0 * x / 2); } }
What does the following print?
public class Main { public static void main(String[] args) { int x = 2 + 'B'; int y = 5; switch (x) { case 'B': System.out.print(1); case 'C': System.out.print(2); case 'D': System.out.print(y + y + "B"); default : System.out.print("B" + y + y); } if ((y > 4) && (y <= 6)) if (y == 6) System.out.print("E"); else System.out.print("F"); } }
What will the following print?
public class Main { public static void main(String[] args) { int s = 0; for (int d = 1; d < 50; d++) { if (50 % d == 0) s += d; } System.out.println(s); } }
What will the following print?
public class Main { public static void main(String[] args) { int s = '\u0052' - '\u0042'; System.out.println(s); } }
What will the following print?
public class Main { public static void main(String[] args) { char c = 'K'; c = (char) (((7 + (c - 'A') * 5) % 26) + 'A'); System.out.println(c); } }
What will the following print?
public class Main { public static void main(String[] args) { double pi = 3.1459; System.out.printf("%.2f + \\\n %.1f" + "\"", pi, pi); } }
If a
and b
are int variables, and b != 0
, circle the choices below that
equal a%b
. (There may or may not be more than one.)
a) a - ( a / b ) * b b) ( a / b ) * b c) a - a / b d) (double) a / b - a / b
What does the following print?
public class Main { public static void main(String[] args) { int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5); System.out.println(sum); } }
Suppose x, y, and z are of type int. Using an appropriate function from the Math class, write a single Java statement which defines a variable named max of type int and assigns it a value equal to the maximum of x, y, and z.
The following program was intended to check the discriminant of the quadratic
equation x^2 + 0.4x + 0.04 = 0
to say something about the number of solutions. The
output printed was "zero or two solutions", when we know there is only one solution
(namely, x = -0.2
). Explain what went wrong.
public class Main { public static void main(String[] args) { double a = 1; double b = 0.4; double c = 0.04; if (b*b - 4*a*c == 0) { System.out.println("one solution"); } else { System.out.println("zero or two solutions"); } } }
Complete the following code so that a triangle of x's whose height and base match the user input is printed, as shown in the sample runs given.
public class Main { public static void main(String[] args) { int n = Integer.parseInt(args[0]); } }
Sample Runs: > java Main 5 <-- Note: this is a command to run the program, not output x xx xxx xxxx xxxxx > java Main 3 <-- Note: this is a command to run the program, not output x xx xxx
byte*, short*, int*, long, float, double, char*, boolean
4
10.3
10001001101
10011100
1, 1.0, 1.0F, true, 'a'
, respectively
final
6ADF0E7
P Q R (P&&Q) (!R) (P&&Q)^(!R) F F F F T T F F T F F F F T F F T T F T T F F F T F F F T T T F T F F F T T F T T F T T T T F T
1 0.5
10BB55F
43
16
F
3.15 + \ 3.1"
6
int max = Math.max(x, Math.max(y,z));
you can't compare doubles with ==
, they are stored approximately
public class Main { public static void main(String[] args) { int n = Integer.parseInt(args[0]); for (int row = 0; row < n; row++) { for (int col = 0; col <= row; col++) System.out.print('x'); System.out.println(); } } }