Midterm Example questions:

Important!!! This is NOT a comprehensive list of topics for the exam. It does NOT cover all the topics required for the midterm. You should refer to the Midterm Review Sheet for a list of topics you are responsible for. This is just a set of exercises provided to you to practice for the midterm.

1. Consider a class Exam. It has the following instance variable:  an integer number of questions: n_questions  a double: duration. It also has the following:  getters and setters  a constructor that takes an integer and a double and assigns them to this object’s instance variables.  a no argument constructor that does not take any arguments and initializes n_questions to 0 and duration to 0.0  a toString method that returns the exam in the following format: This exam has n_questions and takes duration mins  equals method that takes an Exam object. It returns true if both objects have the same n_questions and duration and false otherwise. 2. Write an interface called Doable. It has one constant int DO_TIME = 5 and a void method justDoIt that takes no arguments.

3. What is wrong with the following codes? class Myclass { private int myx = 90; public static int givemeX () { return myx; } }

4. Assume a = 5 and b = 5, what is the output of the following and why?? if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; 5. If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) x *= 2;

6. Use a loop to allow the user to enter 10 numbers, calculates their sum and average.

7. Write a method that returns true if the integer passed as an argument is even and false otherwise.

8. Use the DecimalFormat class to display the number 12356.25555555555 as 12,356.255

9. Write the following equation in Java using loops and the Math class. Assume all the variables used have been declared and initialized properly. 15 y  i x i0

10. Write the statements needed in order to add an image “mypic.gif” to a JPanel called mypanel. Assume that all the packages necessary have been imported and that the image file is placed in the same folder as the java file.

11. Write one line of code that breaks in half a string myname into two pieces. It then combines these pieces in the wrong order in a new string called flipname. For example assume myname = “lory”. Then, flipname = “rylo” .

12. Use the Scanner class to read a file called “myfile.txt” and prints its contents on the screen.