Java Language Fundamentals
Java Language Fundamentals
Java Language Fundamentals
1. Language Fundamentals
Q: 1 Given
10. class Foo {
11. static void alpha() { /* more code here */ }
12. void beta() { /* more code here */ }Page 2 of 14Attention:
13. }
Q: 2 Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x = 1; x < yahoo.length; x++) {
16. System.out.print(yahoo[x] + " ");
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
Q: 3 Given:
15. public class Yippee {
16. public static void main(String [] args) {
17. for(int x = 1; x < args.length; x++) {
18. System.out.print(args[x] + " ");
19. }
20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4
Which code should be inserted at line 1 of Demo.java to compile and run Demo to print
"pizzapizza"?
A. import utils.*;
B. static import utils.*;
C. import utils.Repetition.*;
D. static import utils.Repetition.*;
E. import utils.Repetition.twice();
F. import static utils.Repetition.twice;
G. static import utils.Repetition.twice;
Answer: F
Answer: A, C
Q: 6
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. public static void process(byte[]) { /* more code here */ }
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. process(bytes);
B. BitUtils.process(bytes);
C. util.BitUtils.process(bytes);
D. SomeApp cannot use methods in BitUtils.
E. import util.BitUtils.*; process(bytes);
Answer: C
Q: 7 Given:
enum Example { ONE, TWO, THREE }
Which statement is true?
A. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.
B. The expression (ONE < TWO) is guaranteed to be true and ONE.compareTo(TWO) is guaranteed
to be less than one.
C. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must use a
java.util.EnumMap.
D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because
enumerated types do NOT implement java.lang.Comparable.
Answer: A
Q: 8 Given:
11. public abstract class Shape {
12. private int x;
13. private int y;
14. public abstract void draw();
Which two classes use the Shape class correctly? (Choose two.)
Answer: B, E
Q: 09 Given:
Which code, inserted at line 14, allows the Sprite class to compile?
A. Direction d = NORTH;
B. Nav.Direction d = NORTH;
C. Direction d = Direction.NORTH;
D. Nav.Direction d = Nav.Direction.NORTH;
Answer: D
A. Compilation fails.
B. The code compiles and the output is 2.
C. If lines 16, 17 and 18 were removed, compilation would fail.
Solution:
interface Reloadable{
public void reload();
}
class Edit{
public void edit(){/* Edit Here*/}
}
interface Displayable
extends Reloadable {
public void display();
}
Q:12 Given:
35. String #name = "Jane Doe";
36. int $age = 24;
37. Double _height = 123.5;
38. double ~temp = 37.5;
Q: 13 Given:
55. int [] x = {1, 2, 3, 4, 5};
56. int y[] = x;
57. System.out.println(y[2]);
Q: 14
A programmer needs to create a logging method that can accept an
arbitrary number of arguments. For example, it may be called in these ways:
logIt("log message1");
logIt("log message2","log message3");
logIt("log message4","log message5","log message6");
Which declaration satisfies this requirement?
A. public void logIt(String * msgs)
B. public void logIt(String [] msgs)
C. public void logIt(String... msgs)
D. public void logIt(String msg1, String msg2, String msg3)
Q: 15
Which two code fragments correctly create and initialize a static array of int
elements? (Choose two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Answer: A, B
Q: 16 Given:
11. public static void main(String[] args) {
12. String str = "null";
13. if (str == null) {
14. System.out.println("null");
15. } else (str.length() == 0) {
16. System.out.println("zero");
17. } else {
18. System.out.println("some");
19. }
20. }
A. 5
B. 10
C. 12
D. 17
Q: 18 Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. }
Question: 19
A programmer is designing a class to encapsulate the information
about an inventory item. A JavaBeans component is needed to
do this. The Inventoryltem class has private instance variables to store
the item information:
10. private int itemId;
11. private String name;
12. private String description;
Which method signature follows the JavaBeans naming standards for
modifying the itemld instance variable?
A. itemID(int itemId)
B. update(int itemId)
C. setItemId(int itemId)
D. mutateItemId(int itemId)
E. updateItemID(int itemId)
Answer: C
Question:20
Given a file GrizzlyBear.java:
1. package animals.mammals;
2.
DURGASOFT, # 202,2nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com Page 10
DURGA’s SCJP Material
3. public class GrizzlyBear extends Bear {
4. void hunt() {
5. Salmon s = findSalmon();
6. s.consume();
7. }
8. }
and another file, Salmon.java:
1. package animals.fish;
2.
3. public class Salmon extends Fish {
4. void consume() { /* do stuff */ }
5. }
Assume both classes are defined in the correct directories for theft
packages, and that the Mammal class correctly defines the
findSalmon() method. Which two changes allow this code to compile
correctly? (Choose two.)
A. add public to the start of line 4 in Salmon.java
B. add public to the start of line 4 in GrizzlyBear.java
C. add import animals.mammals.*; at line 2 in Salmon.java
D. add import animals.fish.*; at line 2 in GrizzlyBear.java
E. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java
F. add import animals.mammals.GrizzlyBear.*;at line 2 in Salmon.java
Answer: AD
Question:21
Given:
11. public class Barn {
12. public static void main(String[] args) {
13. new Barn().go("hi", 1);
14. new Barn().go("hi", "world", 2);
15. }
16. public void go(String... y, int x) {
17. System.out.print(y[y.length - 1] + " ");
18. }
19. }
What is the result?
A. hi hi
B. hi world
C. world world
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D
Question:22
Given:
11. class Mud {
12. // insert code here
13. 14. }
System.out.println("hi");
15. }
DURGASOFT, # 202,2nd Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com Page 11
DURGA’s SCJP Material
And the following five fragments:
public static void main(String...a) {
public static void main(String.* a) {
public static void main(String... a) {
public static void main(String[]... a) {
public static void main(String...[] a) {
How many of the code fragments, inserted independently at line 12, compile?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
Answer: D
Question:23
Given:
1. class Alligator {
2. public static void main(String[] args) {
3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
4. int [][]y = x;
5. System.out.println(y[2][1]);
6. }
7. }
What is the result?
A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.
Answer: E
Question:24
1. public class Venus {
2. public static void main(String[] args) {
3. int [] x = {1,2,3};
4. int y[] = {4,5,6};
5. new Venus().go(x,y);
6. }
7. void go(int[]... z) {
8. for(int[] a : z)
9. System.out.print(a[0]);
10. }
11. } What is the result?
A. 1
B. 12
C. 14
Question:25
Which two code fragments correctly create and initialize a static array of int elements?
(Choose two.)
A. static final int[] a = { 100,200 };
B. static final int[] a; static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a; static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Answer: A,B