-
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 and store
values in those variables. (char, short, int, long, float, and double).
For example:
int x; // Declare an integer variable called x
x = 3; // Store the value 3 in the variable x
-
Show how to write a for loop using correct syntax. For example, if
given "Show how to write a for
loop that will print 'Hello, Java World' 10 times." The answer would be:
for(int i=0; i<10; i++)
{
System.out.println("Hello, Java World");
}
-
Show how to write a while loop using correct syntax. For example, if
given "Show how to write a while
loop that will print 'Hello, Java World' 10 times." The answer would be:
int i=0;
while(i < 10)
{
System.out.println("Hello, Java World");
i++;
}
-
Show how to write a do..while loop using correct syntax. For example, if
given "Show how to write a do..while
loop that will print 'Hello, Java World' 10 times." The answer would be:
int i=0;
do
{
System.out.println("Hello, Java World");
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 an if statement that will test true if two
conditions are both true. For example,
"How would you write an if statement to print 'In range' if the int
variable val is greater than or equal to 3 AND less than 10."
The answer would be.
if((val >= 3) && (val < 10)
{
System.out.println("In range");
}
-
Show how to write an if statement that will test true if either of
two conditions are true. For example,
"How would you write an if statement to print 'OK' if the int
variable A is greater than or equal to 3 ORthe int variable
B is less than 10."
The answer would be.
if((A >= 3) || (B < 10)
{
System.out.println("OK");
}
-
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;
}