Download ECE122 Quiz #1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ECE122 Quiz #1
Open books, open notes, No computer.
Question 1:
Which of the following is NOT right for Java programming language?
1.
2.
3.
4.
Object oriented
Platform dependent
Garbage collected
Multi-threaded
Answer
We’ve mentioned that Java is platform-INDEPENDENT several times. This
is one reason Java is so popular to use in web pages and also why it is
so easy for us to allow you to use your home computers. So, by process
of elimination, the answer must be 2.
If you’re interested in the other choices
1 Java is Object oriented. This means you design your code around data,
and decide what operations you can perform on that data. (you'll soon
see that one giveaway is the use of the word "class". Objects are
instances of classes)
3 Garbage collected. This means that any memory you allocate will
automatically be reclaimed by the system when your program no longer
needs it. The non-garbage collected language C requires the programmer
to "free" any memory that that was gotten with "malloc", and C++
requires the programmer to "delete" any memory that was gotten with
"new".
4 Multi threaded. This means a program can have multiple threads of
control. For instance, a web server could assign a separate thread to
each user connected to it . The code to handle a user can be written
and executed more or less as if there were never more than a single
user using the server, relying on Java to give each thread/user a fair
share of the computer.
Question 2:
How do you invoke a Java virtual machine in a window DOS window?
1.
2.
3.
4.
javac
jar
java
None of the above
Answer
The answer is 3, java. You use the java command to have your program
run by a Java virtual machine. The jar program is used to package up
many java class files into a single jar file (something like the zip
utility on Windows). The javac program is the Java Compiler (see the
next question).
Question 3:
What will java compiler NOT do?
1.
2.
3.
4.
Turn Java source code into Byte code
Create java class files
Check syntax errors in the source code
Turn all your code into one archive file
Answer
The answer is 4. The javac Java compiler will take your source code
and create class files from it. Inside the class files, the
instructions you wrote are stored in a simpler format called byte
codes. The compiler can only create a class file if your source code
is syntactically correct. As described in question 3, you use the jar
command to create an archive file.
Question 4:
Why do you need to set up classpath in Java environment?
1. So that the Java virtual machine can find the class files and byte codes to
execute.
2. So that the operating system can find the Java virtual machine to start
3. So that the operating system can find “javac” location
4. So that the operating system can find path environment variable.
Answer
The answer is 1. CLASSPATH is a special variable that the programs that
come with the a Java installation use to find resources, such as class
files, that are specific to Java. The PATH variable is used to find
any programs you might want to run, on both Windows and Unix -- answers
2 and 3. The OS always has access to its PATH variable.
Question 5:
Which of the following can be the starting method that the java virtual machine look for
to execute a typical Java Program?
1.
2.
3.
4.
public static void main(String[] asdf)
public static void main()
public static void main(String args)
Any of the above.
Answer
The answer is 1.
In order, the tokens in the signature mean

public -- is visible outside the class to other classes





static -write for
void
-main
-String[]
asdf
-will hold
does not need an object instance (all methods we'll
a while will be static)
it does not return any values
the special name the Java virtual machine will look for
-- the method takes an array of String objects
essentially declares a local variable named asdf that
the arguments. The name can be anything.
Probably the most common confusion was to think that the parameter had
to be called args. This is just a typical name for the parameter since
it holds the arguments to the program. Also, people often overlooked
that the [] characters are a required part of the signature. See Java
2, page 15.
Question 6:
What are the valid values of a java boolean?
1.
2.
3.
4.
number 1, 0
string “1”, “0”
true, false
any of the above
Answer
The answer is 3. Booleans are only true or false.
Question 7:
What is the result of such program segment?
int i = 4;
int j = 3;
double d = (double) (i/j);
System.out.println(d);
1.
2.
3.
4.
1.33333333
1.0
1
none of the above
Answer
The answer is 2. Expressions in parentheses are always evaluated before
expressions outside parentheses, just as in the arithmetic you learned
in school. So this expression means
First divide the integer 4 by the integer 3 using the integer divide
operation. This gives integer 1. The integer 1 is then cast to the
double value 1.0 and assigned to d. The println method will display
this as “1.0”.
Question 8:
Do I have to write a main method in every class I create?
1. Yes
2. No
Answer
The answer is no. Every program you write must have at least one main
method in some class, since that’s where the program starts running,
but not every class in the program needs one. In fact, usually there is
exactly one main method. For instance, you've used the String class;
it's a utility class representing a text string. It has no main method.
See Head First Java, page 10
Question 9:
Which one will output the following result?
I like
ECE122
1. System.out.print(“I like”);
System.out.println(“ECE122”);
2. System.out.println(“I like\tECE122”);
3. System.out.println(“I like\n”);
System.out.println(“ECE122”);
4. System.out.println(“I like\nECE122);
Answer
The answer is 4. You know two ways to generate a new line in your
output. System.out.println puts one after printing its argument, and
the special escape character \n will insert a new line inside a
string. The special character \t will insert a tab character in a
string (which usually prints as 4 or 8 spaces). The other choices will
print out something like:
1. I like ECE122
2. I like
ECE122
3. I like
ECE122
Question 10:
Which of the answers is right?
boolean b = true;
int i = b;
1. There will be run time error.
2. There will be compile time error.
3. There is no error.
4. Uncertain, there may be an error. If there is an error, I can use try/catch exception
handling to handle this error.
Answer
The answer is 2. Variables in Java are strongly typed and in general
you cannot simply assign values from variables of one type to one of
another type. The compiler will complain and refuse to create a class
file if it sees this error.
A boolean variable can only have the values true and
false, and an int can only contain an integer number. Although Java
will allow you to assign between certain numeric types without a cast,
this is only when no precision could be lost (e.g. an int to a double,
but not a double to an int as this would in general lose the
fractional part of the number). Note that in C and C++ the bool type
is basically considered like an to be a number when it comes to casting
between types. This is not true in Java.