C and Java Programming Fundamentals
Test your knowledge of C and Java programming concepts including bit manipulation, arrays, exception handling, inheritance, interfaces, and abstract classes.
Questions
Given: 1. package sun.scjp; 2. public enum Color { RED, GREEN, BLUE } 1. package sun.beta; 2. // insert code here 3. public class Beta { 4. Color g = GREEN; 5. public static void main( String[] argv) 6. { System.out.println( GREEN); } 7. } The class Beta and the enum Color are in different packages. Which two code fragments, inserted individually at line 2 of the Beta declaration, will allow this code to compile? (Choose two.)
- import sun.scjp.Color.*;
- import sun.scjp.Color; import static sun.scjp.Color.*;
- import sun.scjp.Color; import static sun.scjp.Color.GREEN;
- import static sun.scjp.Color.*;
Given: 1. public interface A { 2. String DEFAULT_GREETING = “Hello World”; 3. public void method1(); 4. } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct?
- public interface B extends A { }
- public interface B implements A {}
- public interface B instanceOf A {}
- public interface B inheritsFrom A { }
Given: 1. class TestA { 2. public void start() { System.out.println(”TestA”); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println(”TestB”); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result?
- TestA
- TestB
- Compilation fails.
- An exception is thrown at runtime.
Given: 1. interface TestA { String toString(); } 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println(new TestA() { 5. public String toString() { return “test”; } 6. }); 7. } 8. } What is the result?
- test
- null
- Compilation fails because of an error in line 1
- Compilation fails because of an error in line 5
Given: 11. public abstract class Shape { 12. int x; 13. int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } and a class Circle that extends and fully implements the Shape class. Which is correct?
- Shape s = new Shape(); s.setAnchor(10,10); s.draw();
- Circle c = new Shape(); c.setAnchor(10,10); c.draw();
- Shape s = new Circle(); s.setAnchor(10,10); s.draw();
- Shape s = new Circle(); s->setAnchor(10,10); s->draw();
Given: 10. abstract public class Employee { 11. protected abstract double getSalesAmount(); 12. public double getCommision() { 13. return getSalesAmount() * 0.15; 14. } 15. } 16. class Sales extends Employee { 17. // insert method here 18. } Which two methods, inserted independently at line 17, correctly complete the Sales class? (Choose two.)
- A. double getSalesAmount() { return 1230.45; }
- B. public double getSalesAmount() { return 1230.45; }
- C. private double getSalesAmount() { return 1230.45; }
- D. protected double getSalesAmount() { return 1230.45; }
Given: 10. interface Data { public void load(); } 11. abstract class Info { public abstract void load(); } Which class correctly uses the Data interface and Info class?
- . public class Employee extends Info implements Data { public void load() { /do something/ }
- public class Employee implements Info extends Data { public void load() { /do something/ }
- public class Employee extends Info implements Data { public void load() { /*do something */ }
- public class Employee implements Info extends Data { public void Data.load() { /*d something */ }
Given: 11. public abstract class Shape { 12. private int x; 13. private int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } Which two classes use the Shape class correctly? (Choose two.)
- public class Circle implements Shape { private int radius;
- public abstract class Circle extends Shape { private int radius;
- public class Circle extends Shape {
- public abstract class Circle implements Shape {
Click the Exhibit button. 1. public class GoTest { 2. public static void main(String[] args) { 3. Sente a = new Sente(); a.go(); 4. Goban b = new Goban(); b.go(); 5. Stone c = new Stone(); c.go(); 6. } 7. } 8. 9. class Sente implements Go { 10. public void go() { System.out.println(”go in Sente.”); } 11. } 12. 13. class Goban extends Sente { 14. public void go() { System.out.println(”go in Goban”); } 15. } 16. 17. class Stone extends Goban implements Go { } 18. 19. interface Go { public void go(); } What is the result?
- go in Goban go in Sente go in Sente
- go in Sente go in Sente go in Goban
- go in Sente go in Goban go in Goban
- D. go in Goban go in Goban go in Sente
public class xyz { public static void main(String args[]) { for(int i = 0; i < 2; i++) { for(int j = 2; j>= 0; j--) { if(i == j) break; System.out.println("i=" + i + " j="+j); } } } } A) i=0 j=0 B) i=0 j=1 C) i=0 j=2 D) i=1 j=0 E) i=1 j=1 F) i=1 j=2 G) i=2 j=0 H) i=2 j=1 I) i=2 j=2
- B,C
- A,I,F
- B,C,F
- None of the above
Select the one correct answer. Which method defined in Integer class can be used to convert an Integer object to primitive int type. A) valueOf B) intValue C) getInt D) getInteger
- A
- C
- D
- None of the Above
public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i == j) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } } A) 0 B) 1 C) 2 D) 3 E) 4
- D
- E
- A
- Both D and E
What is the result of evaluating the expression 14 ^ 23. Select the one correct answer. A) 25 B) 37 C) 6 D) 31 E) 17 F) 9 G) 24
- B
- A
- D
- E
Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object.
- True
- False
In which all cases does an exception gets generated. Select the two correct answers. int i = 0, j = 1;
- if((i == 0) || (j/i == 1))
- if((i == 0) | (j/i == 1))
- if((i != 0) && (j/i == 1))
- if((i != 0) & (j/i == 1))
What would be the output of the following program? main() { char *str[] = {"Frogs","Do","Not","Die.","They",'Croak!"}; printf("%d %d",sizeof(str),sizeoof(str[0])); }
- 6 5
- 6 1
- 12 2
- 5 4
What would be the output of the following program? main() { int a[5] = {2,3};; printf("\n%d %d %d", a[2],a[3],a[4]); }
- Garbage values
- 2 3 3
- 3 2 2
- 0 0 0
What would be the output of the following program? main() { static int a[20]; int i =0 ; a[i]=i++; printf("\n%d %d %d",a[0],a[1],i); }
- 1 0 1
- 0 0 1
- 1 2 3
- Compile error
The maximum combined length of the command line arguments including the spaces between adjacent arguments is
- 128 characters
- 256 characters
- 67 characters
- It may vary from one operating system to another
What would be the output of the following program? main() { unsigned char i = 0x80; printf("\n%d", i<<1); }
- 0
- 256
- 100
- None of the above