El-Shorouk Academy
Higher Institute for Computer &
Information Technology
Dr. Mohamed Mostafa
Acad. Year : 2014 / 2015
Term
: First
Year
: Fourth
Department of CS
Advanced Programming with Java
Sheet # 2 Introduction to Java Programming
1-
What is the output of each of the following println statements?
A- System.out.println("\ta\tb\tc");
B- System.out.println("\\\\");
C- System.out.println("'");
D- System.out.println("\"\"\"");
E- System.out.println("C:\nin\the downward spiral");
A-
a
b
c
B- \\
C- '
D- """
Ein
2-
C:
he downward spiral
Write a println statement to produce the following line of output:
/ \ // \\ /// \\\
System.out.println("/\\//\\\\///\\\\\\");
3-
What println statements will generate the following output?
This program prints a
quote from the Gettysburg Address.
"Four score and seven years ago,
our 'fore fathers' brought forth on this continent
a new nation."
System.out.println("This program print a");
System.out.println("quote from the Gettysburg Address.");
System.out.println("\"Four score and seven years ago,");
System.out.println("our 'fore fathers' brought forth on this continent");
System.out.println("a new nation.\"");
4-
What println statements will generate the following output?
A "quoted" String is
'much' better if you learn
the rules of "escape sequences."
Also, "" represents an empty String.
Don't forget to use \" instead of " !
'' is not the same as "
5-
Write a program to print the following block letters spelling "banana".
Use static methods to capture structure and eliminate redundancy.
BBBBB
B
B
BBBBB
B
B
BBBBB
AAAA
A
A
AAAAAA
A
A
N
N
NNN N
N NNN
N
N
AAAA
A
A
AAAAAA
A
A
N
N
NNN N
N NNN
N
N
AAAA
A
A
AAAAAA
A
A
// Prints Banana Sign.
public class Banana {
public static void main(String[] args) {
drawB();
drawA();
drawN();
drawA();
drawN();
drawA();
}
// draws B character
public static void drawB() {
System.out.println("BBBBB");
System.out.println("B B");
System.out.println("BBBBB");
System.out.println("B B");
System.out.println("BBBBB");
System.out.println();
}
// draw A character
public static void drawA() {
System.out.println(" AAAA");
System.out.println("A A");
System.out.println("AAAAAA");
System.out.println("A A");
System.out.println();
}
// Draws N character
public static void drawN() {
System.out.println("N N");
System.out.println("NNN N");
System.out.println("AAAAAA");
System.out.println("N NNN");
System.out.println("N N");
System.out.println();
}
}
6-
What is a keyword? List some Java keywords.
Keywords have specific meaning to the compiler and cannot be used for
other purposes in the program such as variables or method names.
Examples of keywords are class, static, and void.
7-
Is Java case sensitive? What is the case for Java keywords?
Java source code is case sensitive. Java keywords are always in lowercase.
8-
What is a comment? Is the comment ignored by the compiler? How do
you denote a comment line and a comment paragraph?
Comments are used to document what a program is for and how a program
is constructed. Comments help the programmers or users to communicate
and understand the program. Comments are not programming statements
and are ignored by the compiler. In Java, comments are preceded by two
forward slashes (//) in a line or enclosed between /* and */ in multiple
lines. When the compiler sees //, it ignores all text after // in the same line.
When it sees /*, it scans for the next */ and ignores any text between /*
and */
9-
What is the statement to display a string on the console?
System.out.println(string);
10-
Show the output of the following code:
public class Test {
public static void main(String[] args) {
System.out.println("3.5 * 4 / 2 – 2.5 is ");
System.out.println(3.5 * 4 / 2 – 2.5);
}
}
Output is
3.5 * 4 / 2 – 2.5
is 4.5
11-
What is the Java source filename extension, and what is the Java
bytecode filename extension?
The source file extension is .java and the bytecode file extension is .class.
12-
What are the input and output of a Java compiler?
The input of a Java compiler is a Java source code file and the output is a
Java class file.
13-
What is the command to compile a Java program?
javac is the JDK command to compile a program.
14-
What is the command to run a Java program?
java is the JDK command to run a program.
15-
What is the JVM?
JVM is the Java virtual machine that runs a Java program.
16-
Can Java run on any machine? What is needed to run Java on a computer?
Java can run on any machine with a JVM.
17-
What are syntax errors (compile errors), runtime errors, and logic errors?
Syntax errors are detected by compilers. Runtime errors occur during
execution of the program. Logic errors results in incorrect results.
18-
Give examples of syntax errors, runtime errors, and logic errors.
Syntax errors: If you forget to put a closing quotation mark on a string
Runtime errors: If your program needs to read integers, but the user
entered strings
Logic errors suppose you write a program for computing the perimeter of a
rectangle and you mistakenly write your program so that it computes the
area of a rectangle
19-
If you forget to put a closing quotation mark on a string, what kind error
will be raised?
Syntax error.
20-
If your program needs to read integers, but the user entered strings, an
error would occur when running this program. What kind of error is this?
Runtime error.
21-
Suppose you write a program for computing the perimeter of a rectangle
and you mistakenly write your program so that it computes the area of a
rectangle. What kind of error is this?
Logic error.
22-
Identify and fix the errors in the following code:
1- public class Welcome {
2 -public void Main(String[] args) {
3- System.out.println('Welcome to Java!);
4- }
5- )
Line 2: Main should be main.
Line 2: static is missing.
Line 3: Welcome to Java! should be enclosed inside double quotation marks.
Line 5: The last) should be }.
© Copyright 2025 Paperzz