5 Pages

quiz1-answers

Course: ECE 122, Fall 2009
School: UMass (Amherst)
Rating:
 
 
 
 
 

Word Count: 1267

Document Preview

Quiz ECE122 #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 Weve 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....

Register Now

Unformatted Document Excerpt

Coursehero >> Massachusetts >> UMass (Amherst) >> ECE 122

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Quiz ECE122 #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 Weve 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 youre 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 -static -write for void -main -String[] asdf -will hold is visible outside the class to other classes does not need an object instance (all methods we'll a while will be static) it does not return values any 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 thats 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 intended answer was 4, but because this statement accidentally did not have a double quote character to end string, that choice is not v...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

UMass (Amherst) - ECE - 122
Welcome to Java ProgrammingWhy do I want to take this course? I want to major in EE/CSE. ECE122 is a requirement. Java is hot in Job market. It is useful for my career. My friends are taking this course. I am just curiousECE122 Course Web Sit
UMass (Amherst) - ECE - 122
ECE 122April 14, 2005I/O stream Java performs I/O through streams. A stream is linked to a physical device by the Java I/O system. All stream behave in the same manner, even if the actual physical device is different. Same I/O classes and meth
UMass (Amherst) - ECE - 122
ECE 122April 26, 2005Inner Class An inner class is a class defined within another class. An inner object is bonded with its external object. The inner object has access to all (both public and private) the variables and methods of the external
UMass (Amherst) - ECE - 122
ECE 122Exception Handling Exception handling provides a graceful way of handling exceptional cases and errors. The system will not crash when such exception happens. The control will be transferred to exception handling code, which will correct th
UMass (Amherst) - ECE - 122
ECE122April 28, 2005More Graphics Components JLabel JTextField JCheckBox JRadioButtonJLabel A JLabel displays a single line of read-only text, an image, or both text and an image. Demo Label.javaJTextField & JPasswordField JTextField &
UMass (Amherst) - ECE - 122
Download Eclipse 1. Download Eclipse IDE from www.eclipse.org by clicking on one of the download links on page, http:/www.eclipse.org/downloads/index.php , Choose version 3.01. Choose a site that nears us. Save the file in a folder on your local comp
UMass (Amherst) - ECE - 122
ECE 122 Spring 2005 SampleExam #1 Name_ Student ID_ Discussion Session (circle one): (1) Wednesday 10:10am 11am (2) Wednesday 11:15am 12:05pm (3) Wednesday 1:25pm-2:15pm (4) Wednesday 2:30pm-3:20pm Q1 What is the output of the following code? publi
UMass (Amherst) - ECE - 122
1. Most students did very well this time. Most required functions are implemented, and many completed the extra points section.2. Still there are several groups forgot to submit the supporting classes. 10 points were deducted according t
UMass (Amherst) - ECE - 122
Account Project4= =A36563 100A37395 70A73448A93030 0B15582B30534 100B62000 100B63878 100B87334 60C05516C07107 90C16660C31579 80C76512
UMass (Amherst) - ECE - 122
Account Project6= =A36563A37395 90A73448A93030 100B15582B30534 110B62000 110B63878 100B87334 107C05516C07107 110C16660C31579C76512 110C92057
UMass (Amherst) - ECE - 122
Grading Policy:There are 100 points total for this project: - 10 points are deducted if missing any required files. - The dialog should have the required interface, and the required functions should work properly. (80 points) -
UMass (Amherst) - ECE - 122
Grading Scheme:Grading Crietria: Basic requirement: total 100 points. Extra requirment: total 13 points 1. check duplicated SID 3 points 2. Use array directly 10 pointsIf your program cannot be comp
UMass (Amherst) - ECE - 122
Grading Crietria: Basic requirement: total 100 points. 1. project1a 50 points 2. project1b 50 points Extra requirment: total 10 points 1. readDouble 5 points 2. readString
UMass (Amherst) - ECE - 122
Account Project2=A36563 75A37395A73448A93030 60B15582B30534 105B62000 105B63878B87334 110C05516C07107 103C16660C31579 100C76512 110C92057 100D08322 100D14101D78830 110D847
UMass (Amherst) - ECE - 122
Grading Scheme:Grading Crietria: Basic requirement: 1. Course.java2. Student.java Extra requirment: total 10 points 1. printStudents method (Either a 0 or a 10) If your program cannot be compiled succesfully,
UMass (Amherst) - ECE - 122
Account Project3= =A36563 100A37395 90A73448A93030 110B15582B30534 110B62000 110B63878B87334 100C05516C07107 100C16660C31579 50C76512 85C9
UMass (Amherst) - ECE - 122
General comments:1. Many students did not submit Constant.java, NegatePipe.javaand Pipe.java with their work. Your Programs will not compileunless you submit them. We had to supply the necessary classfiles to compile them. Though, they have not
UMass (Amherst) - ECE - 122
About grading:1. Some students' programs do not compile. According to the grading policy, you should have got 0s. 2. Many students did not submit the SID in their codes. Please do so from next time.3. If there was no proper documentation, you wi
UMass (Amherst) - ECE - 122
General comments:1. Many students did not have graduateDate variable and its set/get methods in Student.java. Some of them did not have set/get methods to access private variables in Student.java/Teacher.java/Course.java.2. Some students did not
UMass (Amherst) - ECE - 122
About grading:1. Some students' programs do not compile. According to the grading policy, you should have got 0s. However, dependingon how well your code was written, we gave 50 - 70 points for your effort. Next time we may strictly follow the
UMass (Amherst) - ECE - 122
Grading Crietria: Basic requirement: 1. Part(a): 50 points. 2. Part(b): 50 points. Extra requirment: total 10 points 1. getUniqueStudentId method to return an unique integer as student ID. If your program doe
UMass (Amherst) - ECE - 122
Account Project5= =A36563A37395 100A73448A93030 100B15582B30534 100B62000 100B63878 65B87334 85C05516C07107 100C16660C31579 95C76512 100C92057 100D08322
UMass (Amherst) - ECE - 122
Grading Crietria: Basic requirement: 1. Part(a): 40 points. 2. Part(b): 60 points. Extra requirment: total 10 points 1. doPipeline method in SingleInputPipe.java If your program does not conform to the require
UMass (Amherst) - ECE - 122
public int dayOfYear() { int total = 0; for (int m=1; m < _month; m+) { total += daysInMonth(_year, m); } return total + _day; }
UMass (Amherst) - ECE - 122
ECE 122Feb. 3, 2005Java Data Types Primitive types. boolean, byte, char, short, int, long, float, double Reference Type. Class.boolean 1 bit of data. Can only be of two value, true or false Declaration: boolean b; Assignment: b = true; b
UMass (Amherst) - ECE - 122
ECE 122Feb. 1, 2005Introduction to EclipseJava Statements Declaration Assignment Method callsDeclaration A variable is like a container that can hold certain content/value. Variable declaration consists of type and name Variable name, by
UMass (Amherst) - ECE - 122
ECE 122March 24Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!Here is my class for catimport java.io.*;/importing java I/O package public class Cat1 { File imageFile;
UMass (Amherst) - ECE - 122
Download Eclipse 1. Download Eclipse IDE from www.eclipse.org by clicking on one of the download links on page, http:/www.eclipse.org/downloads/index.php , Choose version 3.01. Choose a site that nears us. Save the file in a folder on your local comp
UMass (Amherst) - ECE - 122
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-threadedQuestion 2: How do you invoke a Java v
UMass (Amherst) - ECE - 122
ECE 122JList Demo JListDemo.javaMouse Event Handling MouseListener MouseMotionListener MouseInputListener. Extends interfaces of both MouseListener and MouseMotionListener to create a single interfaceMethods of MouseListener public voi
UMass (Amherst) - ECE - 122
ECE 122April 12, 2005Look closer at Animal class We can instantiate an Animal object: Animal a = new Animal(); However, what does such an animal object looks like and behaves?We need Animal class, but not generic animal object We still needs
UMass (Amherst) - ECE - 122
ECE 122Feb. 8, 2005if Statement A condition is an expression that can be either true, or false. if statement allows a program to make a decision based on the value of a condition.Equality Operators = e.g. x=y != e.g. x!=y means x is equal to
UMass (Amherst) - ECE - 122
ECE122Feb 10, 2005Unary Operator An operator that takes only a single operand Plus: + Minus: Cast: (type). E.g. (double)Compound Assignment OperatorOperator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Meaning x=x+y x=x-y x=x
UMass (Amherst) - ECE - 122
ECE 122We know thisCat cat = new Cat(); Animal animal = new Animal();and this? Animal animal = new Cat(); An object reference of super class type points to an object of sub class type. It is legal and it is called polymorphism.Motivation L
UMass (Amherst) - ECE - 122
ECE 122April 19, 2005Are you tired of Console I/O yet? We should have something fun. Something graphical, response with a click of a button, even animations.Java GUI What is GUI? Graphical User Interface It is so intuitive, and easy to use
UMass (Amherst) - ECE - 122
ECE122Feb. 22, 2005Any question on Vehicle sample code?Returning from a method (review) void methods do not return a value to the method caller. Such method will return when its method closing brace is reached, or a return statement is executed
UMass (Amherst) - ECE - 122
ECE122Feb. 17. 2005Introduction to Class and Object Java is an object-oriented language. Javas view of the world is a collection of objects and their interactions. Class is a template, or blueprint to build objects. Objects are instantiated (c
UMass (Amherst) - ECE - 122
ECE 122Exception Handling Exception handling provides a graceful way of handling exceptional cases and errors. The system will not crash when such exception happens. The control will be transferred to exception handling code, which will correct th
UMass (Amherst) - ECE - 122
ECE 122Feb. 15, 2005for Repetition Statement for (int i=0; i<5; i+) System.out.println(i); Generalizationfor(initialization; loopContinuationCondition; increment) statement; Better use integer as counter Demo for statementCommon Programmin
UMass (Amherst) - ECE - 122
ECE 122April 14, 2005I/O stream Java performs I/O through streams. A stream is linked to a physical device by the Java I/O system. All stream behave in the same manner, even if the actual physical device is different. Same I/O classes and meth
UMass (Amherst) - ECE - 122
ECE122March 22, 2005ScopeEncapsulation Encapsulation is a feature of object-oriented world. We don want to put a million lines of code in the t main method to solve a large complex problem. It is too complex to comprehend and difficult to dele
UMass (Amherst) - ECE - 122
ECE 122April 26, 2005Inner Class An inner class is a class defined within another class. An inner object is bonded with its external object. The inner object has access to all (both public and private) the variables and methods of the external
UMass (Amherst) - ECE - 122
ECE 122April 12, 2005Look closer at Animal class We can instantiate an Animal object: Animal a = new Animal(); However, what does such an animal object looks like and behaves?We need Animal class, but not generic animal object We still needs
UMass (Amherst) - ECE - 122
ECE122Feb. 22, 2005Any question on Vehicle sample code?Returning from a method (review) void methods do not return a value to the method caller. Such method will return when its method closing brace is reached, or a return statement is executed
UMass (Amherst) - ECE - 122
ECE 122JList Demo JListDemo.javaMouse Event Handling MouseListener MouseMotionListener MouseInputListener. Extends interfaces of both MouseListener and MouseMotionListener to create a single interfaceMethods of MouseListener public voi
UMass (Amherst) - ECE - 122
ECE122Feb 10, 2005Unary Operator An operator that takes only a single operand Plus: + Minus: Cast: (type). E.g. (double)Compound Assignment OperatorOperator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Meaning x=x+y x=x-y x=x
UMass (Amherst) - ECE - 122
ECE 122Feb. 8, 2005if Statement A condition is an expression that can be either true, or false. if statement allows a program to make a decision based on the value of a condition.Equality Operators = e.g. x=y != e.g. x!=y means x is equal to
UMass (Amherst) - ECE - 122
ECE 122Feb. 1, 2005Introduction to EclipseJava Statements Declaration Assignment Method callsDeclaration A variable is like a container that can hold certain content/ value. Variable declaration consists of type and name Variable name, b
UMass (Amherst) - ECE - 122
ECE122Feb. 17. 2005Introduction to Class and Object Java is an object-oriented language. Javas view of the world is a collection of objects and their interactions. Class is a template, or blueprint to build objects. Objects are instantiated (c
UMass (Amherst) - ECE - 122
ECE 122Feb. 15, 2005for Repetition Statement for (int i=0; i<5; i+) System.out.println(i); Generalizationfor(initialization; loopContinuationCondition; increment) statement; Better use integer as counter Demo for statementCommon Programmin
UMass (Amherst) - ECE - 122
ECE 122March 24Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!Here is my class for catimport java.io.*;/importing java I/O package public class Cat1 { File imageFile;
UMass (Amherst) - ECE - 122
ECE 122We know thisCat cat = new Cat(); Animal animal = new Animal();and this? Animal animal = new Cat(); An object reference of super class type points to an object of sub class type. It is legal and it is called polymorphism.Motivation L
UMass (Amherst) - ECE - 122
ECE 122Feb. 3, 2005Java Data Types Primitive types. boolean, byte, char, short, int, long, float, double Reference Type. Class.boolean 1 bit of data. Can only be of two value, true or false Declaration: boolean b; Assignment: b = true; b =
UMass (Amherst) - ECE - 122
ECE122March 22, 2005ScopeEncapsulation Encapsulation is a feature of object-oriented world. We don't want to put a million lines of code in the main method to solve a large complex problem. It is too complex to comprehend and difficult to dele
UMass (Amherst) - ECE - 122
ECE122March 29, 2005Another Example of Inheritance I want to write classes of medical doctors. One is family practitioner, one surgeon. How many classes do I need? What are they? Can I use inheritance? What should I put in super class? What
UMass (Amherst) - ECE - 122
A1 import java.awt.FlowLayout; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JFrame; public class TwoButtonsInnerClass { JButton button1; JButton button2; public void go() { try{ JFrame frame = new JFrame(); frame.getContent
UMass (Amherst) - ECE - 122
Q1 Write a program that shows a Java Jframe with FlowLayout manager. There are two buttons on the frame initially displayed with text "button". When user clicks on any button, the button text changed to "clicked" Q2 write a class that inherit from th
UMass (Amherst) - ECE - 122
ECE 122 Spring 2005 Exam #1 Name_ Student ID_ Discussion Session (circle one): (1) Wednesday 10:10am 11am (2) Wednesday 11:15am 12:05pm (3) Wednesday 1:25pm-2:15pm (4) Wednesday 2:30pm-3:20pm Open Notes, open books, no computers. Please note: If yo
UMass (Amherst) - ECE - 122
Answer to Exam 1 Question #1 The output of the code is: 0 1 2 This is a straight forward question. It tests your knowledge of "for statement". The counter variable is i, i is initialized to be zero, the loop continuation condition is "i<3", the count
UMass (Amherst) - ECE - 122
Answer to Exam 1 Q1 5 4 3 2 1 Q2 case 3 Q3 should use "continue", not "break" should use i-, not i+ should have i- before continue Correct code: public void q3() { int i = 6; while (i>0) { if (i=3) { i-; continue; } System.out.println(i); i-; } }
UMass (Amherst) - ECE - 122
ECE 122 Spring Semester Exam2 Answer A1:Car:startEngine BMW7:turn BMW7:cruiseControl BMW7:temperatureControlSince BMW7 doesnt override startEngine() method, so it inherits it from Car class. Since BMW7 override turn() method, so the overridden met