Sie sind auf Seite 1von 4

1. Write a program that prints ‘Hello World’ to the screen in your favorite Language.

2. Write a program that asks the user for her name and greets her with her name.

3.Create the equivalent of a four-function calculator. The program should ask the user to
enter a number, an operator, and another number. (Use floating point.) It should then carry
out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the
two numbers. Use a switch statement to select the operation. Finally, display the result.
When it finishes the calculation, the program should ask whether the user wants to do
another calculation. The response can be ‘y’ or ‘n’.
 
Some sample interaction with the program might look like this:  
Enter first number, operator, second number: 10 / 3  
Answer = 3.333333  
Do another (y/n)? y  
 
Enter first number, operator, second number: 12 + 100  
Answer = 112  
Do another (y/n)? n 
 
Point of this quiz​: handling inputs from user, conditional checking, infinite loop

4.
Use for loops to construct a program that displays a pyramid of Xs on the screen. Ask the
user to input number of lines to show. The pyramid should look like this if user input 5.

X
XXX
XXXXX
XXXXXXX
XXXXXXXXX 1

One way to do this is to nest two inner loops, one to print spaces and one to print Xs, inside
an outer loop that steps down the screen from line to line.

Point of this quiz​


: nested loops, conditional checking, verification of user input.

5.
Create a class called time that has separate int member data for hours, minutes, and
seconds. One constructor should initialize this data to 0, and another should initialize it to
fixed values. Another member function should display it, in 11:59:59 format. The final
member function should add two objects of type time passed as arguments.

A main() program should create two initialized time objects (should they be f ​inal​ ?) and
one that isn’t initialized. Then it should add the two initialized values together, leaving the
result in the third time variable. Finally it should display the value of this third variable.
Make appropriate member functions ​ final​ .

Point of this quiz​


: object structure, different modifiers in object members.

6.
Write a program that will read a sequence of positive real numbers entered by the user and
will print the same numbers in sorted order from smallest to largest. The user will input a
zero to mark the end of the input. Assume that at most 100 positive numbers will be
entered.

Point of this quiz​


: manual sorting.

7.
Write a program in Java to print Fibonacci series up to given number? Write both ​
iterative
and ​
recursive​version.

Fibonacci series a popular number series and very popular programming question in Java, in
which number is equal to sum of previous two numbers, starting from third. Fibonacci series
is also a good recursion exercise and often asked in Interviews as well. Try doing this quiz by
using both Iteration (loops) and recursion. For help see H
​ow to print Fibonacci series in Java
using recursion​.

8.
In the last exercise, we will create the following class hierarchy:
For each exercise, you will probably want to write a main method in a separate class to test
the classes you are implementing as you go.

Exercise A: Class Person


Write an ​
abstract​ class Person that i​
mplements​the above methods. A person should have
a given name and a family name. It should also be possible to print a representation of the
person.

Exercise B: Classes Teacher and Student


Write the Teacher and Student classes that ​
inherit​ from Person. In addition to a name,
Students should have a current year in school. Printing a Teacher or a Student should show
this additional information as well.

Exercise C: Class People


Write a class People that inherits from the ​
java.util.ArrayList<Person>​ class. This
class will ​
keep the list of students and teachers.​
It will also be capable of printing a list of
all of the people.

Sorting out people


Now we want to sort people so that we can print them in a ​lphabetical order​. To achieve
this, we will use the method ​
java.util.Collections.sort().​
This method requires that objects be
Comparable (by satisfying the) ​Comparable interface​ .
Exercise D: Interface Comparable<Person>
We want people to be sorted by the their names, family name first. If two people have the
same family name, the two would be ordered by their given names.

Exercise E: Sorting People


Add a method ​
sort()​
to the class People.

Point of this quiz​


: basic inheritance, basic api functions

Das könnte Ihnen auch gefallen