Comp Sci Midterm – Flashcards
Unlock all answers in this set
Unlock answersquestion
The extension name of a Java bytecode file is
answer
.class
question
What are examples of Operating Systems?
answer
Linux, Windows, Mac
question
What are examples of reserved words>
answer
static,public, void, class
question
Java, can run from what Web browser?
answer
applets.
question
Every statement in Java must end with a
answer
semicolon
question
What is an object oriented programming language?
answer
c++, Java, C#
question
Java source code can be executed on a Java virtual Machine
answer
false
question
If a program compiles fine, but it produces an incorrect result then the program suffers,
answer
a logic error.
question
9 Suppose you define a Java class as follows: public class Test { } In order to compile this program, the source code should be stored in a file named
answer
Test.java
question
What method displays a message in a dialog box?
answer
A. JOptionPane.showMessageDialog(null, "Welcome to Java!")? C. JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE)?
question
How do you correctly display We correctly display, "Welcome to Java!"
answer
B. System.out.print("Welcome to Java")? C. System.out.println("Welcome to Java")?
question
What loads the Java bytecode to the memory?
answer
class loader
question
The JDK command to compile a class in the file Test.java is
answer
javac Test.java
question
Who developed Java
answer
Sun Microsystem
question
Java compiler translates Java source code into
answer
Java bytecode
question
What is Architecture Neutral?
answer
Java
question
Java virtual Machine
answer
is a software that interprets Java bytecode
question
You can use
answer
java, to run a java program
question
A compiler
answer
translates high level language program into machine language program
question
.java
answer
the extension name of a java source code file
question
softwares and programs
answer
are instructions to the computer
question
the speed of the cpu may be measured in
answer
gigahertz/megahertz
question
programming style is important because
answer
good programming makes a program more readable. and a good programming style helps reduce errors.
question
a block is enclosed inside
answer
braces
question
25 To use JOptionPane in your program, you may import it using:
answer
A. import javax.swing.*? D. import javax.swing.JOptionPane?
question
What does line 1 define in a program?
answer
A class, Every Java program must have at least one class. Each class have a name. By convention, class names start with an uppercase letter.
question
What does line 2 define in a program?
answer
The main method. The program is executed from the main method. A class may contain several methods. The main method is the entry point where the program begins execution.
question
What does a method contain?
answer
It is constructed of statements. The main method is this program contains the System.out.println statement. This statement displays the string Welcome to Java! on the console (line4). String is a programming term meaning a sequence of characters/ A string must be enclosed in double quotation marks. Every statement in Java ends with a semicolon; known as a statement terminator.
question
What are reserved words or keywords?
answer
they have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name fir the class. Other reserved words in this program, are public, static, and void.
question
What is Line 3?
answer
It is a comment that documents what the program is and how it is constructed. Comments help programmers to communicate and understand the program. They are not programming statements and thus are ignored by the compiler. In java, comments are preceeded by 2 //, on a line, called a line comment. or ecnclosed /* and */ on one or several lines, called a block comment or paragraph comment. When the compiler sees //, it ignores all text on the same line. When it sees /*, it scans for the next */ and ignores any text.
question
What do a pair of curly braces form?
answer
The form a block that groups the program's components. In Java, each block begins with a opening brace {, and ends with a closing }, Every class has a class block that groups the data and methods of the class. Similarly every method has a method block that groups the statement in the method. Blocks can be nested, meaning that one block can be placed within another.
question
Syntax errors,
answer
most common error. ( if a semicolon is missing, a brace is missing, a quotation mark is missing, or a word is misspelled) {}- denotes a block to enclose statements ()= used with methods [] denote an array // precede a comment line "" enclose a string ( sequence of characters) ; marks the end of a statement
question
What do you do if your program has run time errors?
answer
you have to modify it, recompile it, and execute it again.You can use any text editor or IDE to create or edit Java source code file.
question
What happens if you do not have any syntax errors?
answer
The compiler generates a bytecode file with a .class extension. This, the preceding commanding generates a file named Welcome.class. Java is translated into byte code and can be executed by any program or machine.
question
The java language
answer
is a high level language, but java bytecode is a low level language. The bytecode is similar to machine instructions, but is architecture neutral and can run on any platform that has Java virtual Machine. Java bytecode can run on a variety of hardware platforms and operating systems. Java source code is compiled into java bytecode and java bytecode is interpreted by JVM.
question
javac
answer
Welcome.java
question
Welcome.class
answer
java command
question
When executing a program,
answer
The java virtual machine first loads the bytecode of the class memory using a program classed the class order. If your pgroam uses other classes, the class loader dynamically loads them just before they are needed. After a class, is loader, the JVM uses a program called the bytecode verifer to check the validity of the bytecode and to ensure that the bytecode does not violate Java's security restrictions. Java enforces strict security to make sure files are not tampered with.
question
To rewrite a text in a message box you use:
answer
The Joption class option! it is one of the many predefine classed in the Java library. you can use this rather than reinvent the wheel.
question
There are two types of block statements
answer
next line style, and end of line style. The next line style aligns the braces vertically and makes programs easy to read, whereas the end of line style saves space, and avoids errors.
question
What are runtime errors
answer
They are errors that cause a program to terminate abnormally. They occur while a program is running in an environment that detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. An input erros occurs when the program is waiting for the user to enter a value, but the user enters a value that the program cannot handle. For instance, if the program expects to read in a number, but instead the user enters a string, this causes data-type errors.
question
Logic errors
answer
The program does not perform what it is intended to do
question
compile java
answer
javac
question
run java
answer
java command
question
the keyword class introduces, a
answer
class definition, which is included in the block.
question
Main methods are contained in a class.
answer
To run a java program, the program must have a main method. The main method is is the entry point where the program will start execution.
question
Reserved words, or keywords
answer
have a specific meaning to the compiler, and cannot be used for other purposes.
question
specific import
answer
specifies a single class to be imported. Example: import javax.swing.JOptionPane; (Ch. 1 p. 17)
question
wildcard import
answer
imports all classes in a package. Example: import javax.swing.*; (Ch. 1 p. 17)
question
What is the Unicode for the character A is 65. The expression 'A'+1 evaluates to
answer
B. 66
question
5 % 2
answer
1
question
Which of the following is a constant according to Java naming conventions?
answer
COUNT, MAX_VALUE
question
What keyword must be declared to be used as a constant?
answer
final
question
15 % 4 is _____
answer
B. 3
question
What happens if you attempt to add an int, a byte, a long, and a double, the result will be a
answer
double value.
question
public class Test { public static void main(String[] args) { int x = 1? int y = x++ + x? System.out.println("y is " + y)? } }
answer
y is three
question
9 Which of the following assignment statements is incorrect?
answer
A. i == j == k == 1? D. i = 1 = j = 1 = k = 1?
question
What statements are not illegal?
answer
A. short s = 10? C. int t = 23? D. float f = 34?
question
To declare an int variable number with initial value 2, you write
answer
int number=2
question
12 According to Java naming convention, which of the following names can be variables?
answer
C. totalLength A. findArea
question
The assignment operator in Java is
answer
=
question
What value is printed? public static void main(String[] args) { int j = 0? int i = ++j + j * 5? System.out.println("What is i? " + i)? } }
answer
6
question
15 Analyze the following code. import javax.swing.*? public class ShowErrors { public static void main(String[] args) { int i? int j? String s = JOptionPane.showInputDialog(null, "Enter an integer", "Input", JOptionPane.QUESTION_MESSAGE)? j = Integer.parseInt(s)? i = (i + 4)? }
answer
D. The program cannot compile because i does not have an initial value when it is used in i = i + 4?
question
Every letter in Java is lowercase
answer
true
question
a variable may be assignment a value only once in a program
answer
false
question
Which of the following statements are correct?
answer
A. char c = 100? C. char c = 'd'?
question
A java statement ends with a semicolon
answer
true
question
To declare a constant MAX_LENGTH inside a method with value 99.98, you write
answer
C. final double MAX_LENGTH = 99.98?
question
the value of a variable can be changed
answer
true
question
The __________ method displays an input dialog for reading a string.
answer
A. String string = JOptionPane.showInputDialog("Enter a string")? D. String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE)? E. String string = JOptionPane.showInputDialog(null, "Enter a string")?
question
The following code fragment reads in two numbers: Scanner input = new Scanner(System.in)? int i = input.nextInt()? double d = input.nextDouble()? What are the correct ways to enter these two numbers?
answer
A. Enter an integer, a space, a double value, and then the Enter key. B. Enter an integer, an Enter key, a double value, and then the Enter key. D. Enter an integer, two spaces, a double value, and then the Enter key.
question
25 What is "Welcome" + 1 + 1*2?
answer
D. Welcome12
question
A constant can be defined using using the final keyword.
answer
true
question
Suppose x is a char variable with a value 'b'. What will be displayed by the statement System.out.println(++x)?
answer
c
question
Are the following four statements equivalent? number += 1? number = number + 1? number++? ++number?
answer
yes
question
Which of the following are not valid assignment statements?
answer
55=x;
question
5 Which of the following assignment statements is correct to assign character 5 to c?
answer
B. char c = '5'?
question
You can assign the value in ________ to an int variable.
answer
B. 93 C. 'x'
question
What is x after the following statements? int x = 2? int y = 1? x *= y + 1?
answer
D. x is 4.
question
8 Which of the following are the same as 1545.534?
answer
A. 1545534.0e3 B. 154553.4e2 C. 1.545534e+3 D. 0.1545534e+4
question
The expression 4 + 20 / (3- 1) * 2 is evaluated to
answer
24
question
To declare a constant PI, you write
answer
D. final double PI = 3.14159?
question
The __________ method parses a string s to a double value.
answer
C. Double.parseDouble(s)?
question
12 Which of the following are correct names for variables according to Java naming conventions?
answer
B. radius C. findArea
question
-15 % 4 is _____
answer
D. -3
question
You can cast a character value to an int or an int to a char
answer
true
question
15 A variable may be assigned a value only once in the program.
answer
false
question
17 What is the exact output of the following code? double area = 3.5? System.out.print("area")? System.out.print(area)?
answer
area3.5
question
18 What is the value of (double)5/2?
answer
2.5
question
If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value.
answer
double