-
Write a simple Java program with correct syntax. An example of this
would be the simple HelloWorld program. Make sure you have syntax
correct in all statements, that curly braces and parentheses are correct,
that semicolons are in the correct locations, that there is a main()
method with correct arguments inside the class definition.
-
Declare variables of any of the primitive/atomic data types (char, short,
int, long, float, and double), and you should know how to store
values in those variables. You should also know the type and ranges
of values for each data type, e.g. int can only hold whole numbers
in the range from about minus 2 billion to plus 2 billion, it cannot
hold numbers like 1.25.
Examples:
int x; // Declare an integer variable called x
x = 3; // Store the value 3 in the variable x
double d; // Declare a double variable called d
d = 3.14159; // Store the value 3.14159 in the variable d
-
Show how to write a for loop. For example, if given "Write a for
loop that will print 'Hi there' 10 times." The answer would be:
for(int i=0; i<10; i++)
{
System.out.println("Hi there");
}
-
Show how to write a while loop. For example, if given "Write a while
loop that will print 'Hi there' 10 times." The answer would be:
int i=0;
while(i < 10)
{
System.out.println("Hi there");
i++;
}
-
Show how to write a do..while loop. For example, if given "Write a do..while
loop that will print 'Hi there' 10 times." The answer would be:
int i=0;
do
{
System.out.println("Hi there");
i++;
}
while(i < 10);
-
Show how to write an if statement. For example,
"How would you write an if statement to print 'Correct' if the int
variable ans is equal to 3." The answer would be.
if(ans == 3)
{
System.out.println("Correct");
}
-
Show how to write an if...else statement. For example,
"How would you write an if..else statement to print 'Correct' if the int
variable ans is equal to 3, but print 'Incorrect' if it is any other
value." The answer would be.
if(ans == 3)
{
System.out.println("Correct");
}
else
{
System.out.println("Incorrect");
}
-
Show how to write a switch statement. For example,
"How would you write a switch statement to print 'Cold' if the int
variable val is equal to 1, 'Cool' if val is 2, 'Warm' if
val is 3, 'Hot' if val is 4, but 'Invalid answer' if val
is any other value. The answer would be.
switch(val)
{
case 1 :
System.our.println("Cold");
break;
case 2 :
System.our.println("Cool");
break;
case 3 :
System.our.println("Warm");
break;
case 4 :
System.our.println("Hot");
break;
default :
System.our.println("Invalid answer");
break;
}