Chapter 1 Getting Started – absolute java
Download file with the answers
Chapter 1 Getting Started - absolute java
1 file(s) 97.84 KB
Not a member!
Create a FREE account here to get access and download this file with answers
Chapter 1
Getting Started
Multiple Choice
1) Java is an object-oriented programming language. An object-oriented language
(a) Uses structured programming.
(b) Views a program as consisting of objects which communicate through interactions.
(c) Functionally breaks down problems into smaller, more manageable problems.
(d) All of the above.
2) In Java, the equal sign is used as the ___________ operator.
(a) increment
(b) decrement
(c) assignment
(d) negation
3) In Java, source code is compiled into object code called ______________.
(a) Bit-code
(b) Class code
(c) Method code
(d) Byte-code
4) The hardest kind of error to detect in a computer program is a:
(a) Syntax error
(b) Run-time error
(c) Logic error
(d) All of the above
5) Identify the invalid Java identifier.
(a) 1Week
(b) Week1
(c) amountDue
(d) amount_due
6) What is the value of the variable amountDue?
double price = 2.50;
double quantity = 5;
double amountDue = 0;
amountDue = price * quantity;
(a) 12
(b) 12.25
(c) 12.5
(d) 13
7) What is the value of 7.52e-5?
(a) 752000.0
(b) 0.0000752
(c) 0.000752
(d) 0.00752
8) What is the Java expression for 4a2 + 2b * c?
(a) (4 * a) + (2 * b) * c
(b) (4 * a * a) + ((2 * b) * c)
(c) ((4 * a * a) + (2 * b)) * c
(d) (4 + a * a) + ((2 + b) * c)
9) What is the Java expression for 27xy?
(a) 27 + (x * y)
(b) 27 * (x + y)
(c) 27 * x * y
(d) 27x * y
10) The value of the expression (int) 27.6 evaluates to:
(a) 28
(b) 27
(c) 26
(d) None of the above.
11) Which operator is used to concatenate two strings?
(a) +
(b) –
(c) *
(d) /
12) Which operator returns the remainder of integer division?
(a) %
(b) /
(c) *
(d) none of the above
13) What is the value of the variable c in the statements that follow?
String phrase = “Make hay while the sun is shining.”;
char c = phrase.charAt(10);
(a) w
(b) h
(c) i
(d) None of the above
14) The escape sequence the represents the new-line character is:
(a) \r
(b) \t
(c) \n
(d) \\
15) The syntax that declares a Java named constant named SALES_TAX is:
(a) double SALES_TAX = 7.50;
(b) public double SALES_TAX = 7.50;
(c) public static double SALES_TAX = 7.50;
(d) public static final double SALES_TAX = 7.50;
16) In Java, a block comment is delimited by:
(a) */ /*
(b) /* /*
(c) /* */
(d) */ */
17) To mark a block comment for inclusion in the Javadoc documentation, the block must be delimited by:
(a) /** */
(b) */* */
(c) **/ /*
(d) **/ */
True/False
1) Java began as a language for home appliances.
2) Applets were designed to run as stand-alone applications.
3) The Java programming language allows you to concatenate two strings using the plus sign.
4) Java is an interpreted language.
5) Java does not require that a variable be declared before it is used within a program.
6) A variable of type boolean can be explicitly converted to that of type int.
7) The modulus operator, %, returns the remainder of integer division.
8) The result of integer division is truncated in Java.
9) Objects of type String are strings of characters that are written within single quotes.
10) In Java, Strings are immutable objects. Immutable objects can be changed.
11) An advantage of using the Unicode character set is that it easily handles languages other than English.
12) Java uses the ASCII character set.
Short Answer/Essay
1) Define high-level languages, machine language and low-level language. Explain how the languages correspond to one another.
Answer: High-level programming languages were designed to be easy for humans to read and use. High-level languages consist of English like statements. Machine language or low-level language is the language that the computer understands directly.
The source code created with Java, a high-level language, is compiled or translated into the machine language, or object code. The object code is the program that executes on the computer.
2) Two kinds of Java programs are applications and applets. Define and discuss each.
Answer: An application is just a regular program that runs on your computer. An applet is a little Java program that runs in a Web browser. Applets and applications are almost identical. The difference is that applications are meant to be run on your computer like any other program, whereas an applet is meant to be run from a Web browser. An applet can be sent to another location on the Internet and run there.
3) What is byte-code? What is its importance?
Answer: Byte-code is the object code the Java compiler creates. Byte-code executes on the Java Virtual Machine (JVM). The JVM is platform independent, that is, not tied to one particular computer. The JVM is a fictitious computer, common to all computers; this feature makes Java unique in that the program can be created on one platform and executed on any other platform.
4) What is the syntax and semantics of a programming language?
Answer: The syntax of a language describes the arrangement of words and punctuations that are legal in the language. The syntax of a language is synonymous with the grammar of a language. The semantics of a language describes the meaning of things written while following the syntax rules of the language. The syntax describes how you write a program and the semantics describes what happens when you run the program.
5) What steps must the programmer take to create an executable Java program?
Answer: First, the programmer must create the source code. The programmer creates a Java class that is contained in a file with the same name as the class. The source code file ends with the .java extension. Next, the programmer uses a tool called a compiler to translate the source code into Java byte-code. If the source code is syntactically correct, a file containing the byte-code is created. This file has the same name as the source code file with one exception, the byte-code file ends in the .class extension.
Once the .class is generated, it may be executed on any computer that has the Java Virtual Machine (JVM) installed on it.
6) List the primitive data types Java supports. Indicate the kind of values each type can store.
Answer: boolean , true or false
char, single Unicode character
byte, integer (8 bits)
short, integer (16 bits)
int, integer (32 bits)
long, integer (64 bits)
float, floating-point number (32 bit IEEE 754)
double, floating-point number (64 bit IEEE 754)
7) How is the % (modulus) operator used? What is the common use of the modulus operator?
Answer: The modulus operator is used to return the remainder in integer division. For example, the modulus operator is commonly used to determine if a number is an even or an odd value.
8) What are the values of the variables a, b, c, and d after the execution of the following expressions?
int a = 3;
int b = 12;
int c = 6;
int d = 1;
d = d * a;
c = c + 2 * a;
d = d – b / c;
c = c * b % c;
b = b / 2;
Answer:
a: 3
b: 6
c: 0
d: 2
9) Explain the difference between an implicit type cast and an explicit type cast.
Answer: A type cast takes a value of one type and produces a value of another type. Java supports two kinds of type casts: explicit and implicit. Java performs an implicit type cast automatically. This can be seen in the declaration of a variable of type double that is assigned an integer value.
double castExample = 72;
The integer value assigned to the variable castExample is automatically converted to a floating point number. The number assigned to castExample is converted to 72.0.
An explicit type cast occurs when the programmer explicitly forces a value of one type into a value of another type. In the example that follows, the value 72.5 is explicitly cast into an integer value.
int castExample = (int) 72.5;
10) What is the output produced by the following lines of code?
int value1 = 3;
int value2 = 4;
int result = 0;
result = value1++ * value2–;
System.out.println(“Post increment/decrement: ” + result);
result = ++value1 * –value2;
System.out.println(“Pre increment/decrement: ” + result);
Answer:
Post increment/decrement: 12
Pre increment/decrement: 10
11) Define the terms class, object, method and method call.
Answer: Objects are entities that store data and can take actions. A class is the name for a type whose values are objects. Methods are defined as the actions that an object can take. A method call is invoked with the dot operator by the calling object, usually a variable of some type. Any information, called arguments, needed by the called method is passed in parenthesis. Objects have a collection of methods.
12) What does the String method trim() do? Give an example of its use.
Answer: The string method trim removes leading and trailing white space, as well as the tab and new-line character from String objects. For example, the following is an invocation of trim():
String s = userInput.trim();
where s represents the new String with no white space, tab or new-line characters.
13) What is the output of the following Java statements?
//String method examples
String str = “Java Programming!”;
System.out.println(str.equals(“Java Programming!”));
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
System.out.println(str.substring(5,8));
System.out.println(str.lastIndexOf(“m”));
Answer:
true
java programming!
JAVA PROGRAMMING!
Pro
12
14) Write a Java statement to access the 7th character in the String variable myString and place it in the char variable c.
Answer:
c = myString.charAt(6);
15) Write a Java statement to determine the length of a string variable called input. Store the result in an integer variable called strLength.
Answer: int strLength = input.length();
16) Why is using named constants a good programming practice?
Answer: Using named constants is a good programming practice because it is less prone to the introduction of bugs into the source code. A retail program that calculates customer purchases needs to include sales tax on the amount of purchase. The sales tax percentage seldom changes, therefore using a named constant whose value is set once reduces the risk of the programmer mistyping the percentage, which would in turn introduce a logic error, the hardest error to track.
17) How are line comments and block comments used?
Answer: A line comment is included as documentation to the programmer or a programmer that might need to modify the code at some later date. A line comment is signified by //. Anything that follows the // is considered a comment by the compiler until the end of the line is reached.
A block comment is included as documentation to users of the class. Users of the class are usually other programmers. A block comment begins with /* and ends with */. A block comment my span many lines. Any text between the beginning /* and the ending */ is part of the block comment. A special block comment can be used with Javadoc, a documentation tool included in Java’s SDK, to create documentation for users of the class.
Leave a reply