Back to feed
Renewal·서른의 생활코딩

Day 4) Variable Review Exercises _2018-05-31 am

NS
normalstory
cover image

package z_exam;

public class exam02 {

public static void main(String[] args){

/*[2-1] Fill in the blank cells of the following table with the 8 primitive types in the appropriate positions.*/

System.out.println(" [Answer 1]");

System.out.println(" 1bit 2byte 4byte 8byte ");

System.out.println(" boolean type boolean ");

System.out.println(" character type char");

System.out.println(" integer type byte short int long");

System.out.println(" floating point float double");

System.out.println("\n");

/*[2-2] I want to store a resident registration number as a "number".

What data type should I choose to store this value?

Declare a variable named regNo and initialize it with your own resident registration number in a single line of code. */

long regNo = 820528123456L ;

// Declare a variable named regNo. If you don't use L, it is recognized as int. Since it exceeds the range, it produces an error.


System.out.println(" [Answer 2]");

System.out.println(" regNo : " + regNo);

System.out.println("\n");

//extra thought 01

String reg2 = "820528";

String No2 = "1*****";

String regNo2 = reg2 + No2; // declare a variable named regNo

System.out.println(" //+ extra thought 1 : " + regNo2);

//extra thought 02 — number + character by data type

int reg3 = 820528;

byte sex3 = 1;

String No3 = "*****";

String regNo3 = sex3 + No3; // declare a variable named regNo

System.out.println(" //+ extra thought 2 : " + reg3 + regNo3);

//extra thought 03 — number + character by data type

int regNo4 = 820528; // declare a variable named regNo

char sex4 = '1';

String No4 = "*****";

System.out.println(" //+ extra thought 3 : " + regNo4 + "-" + sex4 + No4);

System.out.println("\n");

/*[2-3] In the following sentences, identify the literals, variables, constants, and keywords (//reserved words).

int i = 100;

long l = 100L;

final float PI = 3.14f; */

System.out.println(" [Answer 3]");

System.out.println(" Literals: 100 (integer), 100L (integer), 3.14f (floating-point) ");

System.out.println(" Variables: i, l ");

System.out.println(" Keywords: int, long, float, final ");

System.out.println(" Constants: PI ");

System.out.println("\n");

/*[2-4] Which of the following is not a primitive type?

a. int

b. Byte

c. double

d. boolean*/

System.out.println(" [Answer 4]");

System.out.println(" 'b' — because 'it could be a class, a variable, or a value.' ");

System.out.println("\n");

/*[2-5] Write the output of each of the following statements. If there is an error in the statement, write 'error' in the parentheses. */

System.out.println(" [Answer 5]");

System.out.println(" System.out.println(“1” + “2”) → ( 12 ) ");

// After type conversion, the operation is performed. The JVM converts any data type smaller than 4 bytes to 4 bytes.

System.out.println(" System.out.println(true + “”) → ( "true" ) ");

// Since a type conversion occurs (to String, the larger one),

// -> it becomes "true" as a String.

// Because the JVM operand stack stores operands in 4-byte units,

// any data type smaller than 4 bytes

// is converted to 4-byte form 'at the time of operation'.

System.out.println(" System.out.println(‘A' + 'B') → ( 131 ) ");

//= Because char is automatically type-converted -> 65 + 66

System.out.println(" System.out.println('1' + 2) → ( 51 ) ");

//= char 1 + 2 = 49 + 2 (if only one is smaller than 4 bytes, follow the larger value)

System.out.println(" System.out.println('1' + '2') → ( 99 ) ");

//= char 1 + char 2 = 49 + 50

System.out.println(" System.out.println('J' + “ava”) → ( "Java" ) ");

// After type conversion (to String, the larger one) -> the operation is performed

System.out.println(" System.out.println(true + null) → ( error ) ");

//= boolean + “null” of reference type = you cannot add a primitive type to the primitive version of a reference type.

// null =! String

System.out.println("\n");

/*[2-6] Which of the following is NOT a keyword? (Select all)

a. if

b. True

c. NULL

d. Class

e. System*/

System.out.println(" [Answer 6]");

System.out.println(" (b) True -> true is the reserved word; (c) NULL is a value -> null is the reserved word; (d) Class is just text and a recommended class name -> class is the reserved word; (e) System is originally a class.");

System.out.println("\n");

/*[2-7] Which of the following can be used as a variable name? (Select all)

a. $ystem

b. channel#5 // only $ and _ are allowed as special characters

c. 7eleven // a number cannot come at the front

d. If // starting with uppercase is for a class (recommended); lowercase 'if' is a reserved word

e. Korean text // not recommended

f. new

g. $MAX_NUM

h. hello@com // only $ and _ are allowed as special characters */

System.out.println(" [Answer 7]");

System.out.println(" a, d (recommended), e (not recommended), f, g " );

System.out.println("\n");

/*[2-8] Which primitive types are the same size as a reference type variable? (Select all)

a. int

b. long

c. short

d. float

e. double*/

System.out.println(" [Answer 8]");

System.out.println(" 'a, d' because 'a reference type variable is 4 bytes'");

System.out.println("\n");

/*[2-9] Which of the following can omit the type cast? (Select all)

byte b = 10; //8bit(byte)

char ch = 'A'; //16bit(char) *note: only char -> int is allowed

int i = 100; //32bit(int)

long l = 1000L; //64bit(long)

a. b = (byte)i; //8bit(byte) < 32bit(int)

b. ch = (char)b; //16bit(char) ! 8bit(byte)

c. short s = (short)ch; //8bit(short) ! 16bit(char)

// The cast operator does not solve overflow; it is just a warning.

d. float f = (float)l; //32bit(float) > 64bit(long)

e. i = (int)ch; //32bit(int) > 16bit(char) */

System.out.println(" [Answer 9]");

System.out.println(" d and e. ");

System.out.println("\n");


/*[2-10] What is the range of integer values that can be stored in a char type variable? (Write in decimal)*/

System.out.println(" [Answer 10]");

System.out.println(" 2^16-1");

// Since it is a character, it starts from 0; char = 2 bytes = 16 bits =

// 2^16-1

System.out.println("\n");

/*[2-11] Which of the following are incorrectly initialized? (Select all)

a. byte b = 256; //x 8bit = up to 127 allowed ????

b. char c = ''; // a 'primitive type variable' requires a value. Missing, so error.

// & If there's a space bar, it's recognized as '126')

c. char answer = 'no'; // x only one character allowed

d. float f = 3.14 // x without the 'f' suffix, it is recognized as 3.14 (double). And there's no ;

e. double d = 1.4e3f; // o different data types but -> direction allows omitting the cast ()

//* Reference: power-of-ten (Exponential Format) - http://www.allcalc.tk/2383 */

System.out.println(" [Answer 11]");

System.out.println(" 'a,b,c,d' because 'it's type conversion, but in the -> direction () can be omitted' ");

System.out.println("\n");

/*[2-12] Which of the following are correct declarations for a main method? (Select all)

a. public static void main(String[] args)

b. public static void main(String args[])

c. public static void main(String[] arv)

d. public void static main(String[] args)

e. static public void main(String[] args)*/

System.out.println(" [Answer 12]");

System.out.println(" 'a' because 'it's familiar' ");

System.out.println("\n");

/*[2-13] Which of the following type-default value pairs are incorrect? (Select all)

a. boolean - false

b. char - '\u0000'

c. float - 0.0

d. int - 0

e. long - 0

f. String - "" */

System.out.println(" [Answer 13]");

System.out.println(" 'c, e' because it's '0.0f' and long's default is 0.0L");

// The default value of a reference type (String) is null because there is no address.

System.out.println("\n");

/* Reference */

System.out.println(Integer.SIZE);

// The size of a reference type variable is 4 bytes ** https://dzone.com/articles/java-getting-size-object

}

}



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min