Download Document

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
1. Identify the variables that are both valid in Java and follow
its naming conventions:
NumCheques
typeoffile
2ndCard
cardOne
2. Which of the following are primitive types?
int
char
String
Board
boolean
3. What is the assignment operator in Java?
Answer: =
4. Use 2 lines to declare and initialize a double value
called 'weight' to 70.55. Then do it on one line.
Answer:
1) double weight;
weight = 70.55;
2) double weight = 70.55;
5. What is the value of arbitraryNumber at the end of the
following code?
int arbitraryNumber = 4;
arbitraryNumber++;
arbitraryNumber *= 2;
arbitraryNumber % 6;
arbitraryNumber = arbitraryNumber/3;
Answer: 3
6. What are the values of steps in each of the 2 cases
below?
double steps;
steps = 7/2; Answer: 3.0 (changed on Feb 20th, 12:20pm)
steps = 7.0/2; Answer: 3.5
7. What is the value of arbitraryString at the end of the
following code?
String s1 = “Use Google”;
String s2 = “Code sensibly”;
String s3 = s1.substring(4,6);
String arbitraryString = s3 + s2.substring(4,9) + “ ” + s3;
Answer: Go sens Go
8. What is the value of arbitraryString at the end of
the following code?
String arbitraryString = "ABC";
arbitraryString += arbitraryString.charAt(2);
arbitraryString.toLowerCase();
Answer: abcc
9. What will the following code print? Why?
char first = ‘a’;
char second = ‘b’;
System.out.println((char) (first + second));
Answer: 195
10. What will the following lines of code print?
String arbitraryString = "\"Hi\" how\nare you?"
System.out.print(arbitraryString);
Answer:
“Hi” how
are you?
11. What will the following lines of code print if the user
enters …
‘ Hello
How are you? ’
… when prompted for input?
Scanner in = new Scanner(System.in);
String first = in.next();
String second = in.next();
String third = in.nextLine();
System.out.print(first + second + third);
Answer: ‘HelloHow are you? ’
12. Assume isGameOver is a boolean variable that has
been initialized and declared. What is a better way to
write the following lines of code?
if(isGameOver)
{
} else
{ System.out.println("Take your turn.");
}
Answer:
if (!isGameOver)
{
System.out.println(“Take your turn.”);
}
13. When is it appropriate to use .equals() instead of ==?
Answer: .equals() for when you’re comparing objects,
and == for comparing primitive types (int, char, boolean, etc)
14. What is the output of the following lines of code?
String one = "Hello";
String two = "hello";
System.out.println(one.equals(two));
System.out.println(one.equalsIgnoreCase(two));
System.out.println(!one.equalsIgnoreCase(two));
System.out.println(one.equals(two) &&
one.equalsIgnoreCase(two));
System.out.println(one.equals(two) ||
one.equalsIgnoreCase(two));
Answer:
false
true
false
false
true
15. What is the output of the following lines of code?
for (int num = 2; num <= 7; num++)
{
if (num % 2 == 0)
{
System.out.println(“A”);
} else if (3 <= num && num <= 5)
{
System.out.println(“B”);
} else if (num % 3 == 0)
{
System.out.println(“C”);
} else
{
System.out.println(“D”);
}
}
Answer:
A
B
A
B
A
D
16. What does the following code print?
boolean a = true;
boolean b = false;
boolean c = true;
System.out.println((a&&b)||(!b&&c));
System.out.println(((a&&b)||(!b&&c)) == b);
Answer:
true
false
17. When is it best to use a for loop? What about a while loop?
Answer: for loop whenever you have an idea of how
many iterations you want (either explicit, or implicit); use a
while for any other case
18a. How many times is Hello printed?
for(int i = 0; i < 5; i++)
{ System.out.print("Hello");
}
Answer: 5
18b. What about in the following loop?
for(int i = 100; i < 105; i++)
{ System.out.print("Hello");
}
Answer: 5
19. What would the following code do? Assume myString is
initialized and declared.
int position = myString.indexOf(“ ”);
while (position != -1)
{
String firstHalf = myString.substring(0,
position);
String secondHalf = myString.substring(position
+ 1);
myString = firstHalf + secondHalf;
position = myString.indexOf(“ ”);
}
Answer: remove all spaces, i.e.
Hello how are you
Hellohow are you
Hellohoware you
Hellohowareyou
20. Suppose we want to continually prompt the user for
input until we receive some particular String. What
type of loop should we use?
Answer: while loop; e.g. if the string is “quit”, we could
say:
String in = input.nextLine();
while (!in.equals(“quit”))
{
in = input.nextLine();
} // will stop asking for input when
in.equals(“quit”) is true
21. What does the following code do? Assume number is
initialized and declared.
for (int i = 2; i <= number; i = i * 2)
{
System.out.println(i);
}
Answer: prints all powers of 2 up to number
22. What does the following code do? Assume number is
initialized and declared.
for(int i = 1; i <= number; i++)
{ if(number%i == 0)
{ System.out.println(i);
}
}
Answer: prints all factors of number
23. What does the following code do?
Scanner in = new Scanner(System.in);
String input = “”;
while (input.indexOf(‘!’) == -1)
{
input += in.next();
}
System.out.println(input);
Answer: print out the user’s input without spaces or
whitespace until the input includes an “!”
24. What does instantiating an object mean?
Answer: creating an instance of a class. For example,
“graph = new Board(5, 5);” is instantiating a Board object
25. Suppose we have an object called a GameBoy that can be
instantiated without any parameters. Write code to create a
GameBoy object referenced by the variable myGameBoy.
Answer: GameBoy myGameBoy = new GameBoy();
26. What is the function of an instance variable?
Answer: an instance variable contains some
information specific to the object. E.g. a Person object might
have instance variables called age, height, and weight
27. Which methods in a class have access to the instance
variables within that class?
Answer: ALL methods
28. What would we type to create an instance variable
called columns and to initialize it to 5?
Answer: int columns = 5;
29. What does it mean for a method to be void?
Answer: requires no return value
30. What is the return type of the method next() from
the Scanner class?
Answer: String
31. What does the following code perform?
Scanner in = new Scanner(System.in);
String newString = in.next().toUpperCase();
System.out.println(newString);
Answer: transforms the input string into uppercase
(e.g. HeLlo how arE yOu -> HELLO)
32. True or false: Void methods must always have return
statements.
Answer: false; void methods don’t return anything
33. What does ‘this’ refer to when it is used in a method?
Answer: current object
34. What happens if we run the following lines of code?
if(3 < 4)
{ int a = 8;
}
System.out.println(a);
Answer: 8
35. Consider the following method signature. Is position a
parameter or an argument?
public char charAt(int position)
Answer: parameter
36. Consider the following line of code (Assume
position is an int and myString is a String, both of
which have been initialized and declared). Is position
a parameter or an argument?
char c = myString.charAt(position);
Answer: argument
37. What are the pre and post conditions for the charAt(int
index) method of the String class?
Answer: Pre: 0 <= index < this.length();
Post: return character of this at position index
38. What is the difference between the visibility
modifiers 'public' and 'private'?
Answer: public means that the variable or method can
be seen by anyone (from inside the class or outside); private
means that the variable or method can ONLY be accessed
within its own class
39. Is the method getRows() of the Board class an accessor
method, mutator method, or neither?
Answer:
accessor
40. Is the method makePayment(int payment) of the
CreditCard class an accessor method, mutator method, or
neither?
Answer: neither. As a general guideline, accessor
methods usually start with “get” and mutator methods start
with “set”
41. What is the output of the following lines of code?
String originalString = “high”;
String s1 = originalString.substring(0,2);
String s2 = “hi”;
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
Answer:
false
true