Quiz
Джава
Create a free account to finish this quiz
Save your progress and see your final score.
Already have an account? Sign in
Questions in this quiz (40)
1 What modifiers are implicitly applied to all interface methods? (Choose all that apply)
- Aprotected
- Bpublic
- Cstatic
- Dvoid
- Eabstract
- Fdefault
2What is the output of the following code?
1: class Mammal {
2: public Mammal(int age) {
3: System.out.print("Mammal");
4: }
5: }
6: public class Platypus extends Mammal {
7: public Platypus() {
8: System.out.print("Platypus");
9: }
10: public static void main(String[] args) {
11: new Mammal(5);
12: }
13: }- APlatypus
- BMammal
- CPlatypusMammal
- DMammalPlatypus
- EThe code will not compile because of line 8.
- FThe code will not compile because of line 11.
3Which of the following statements can be inserted in the blank line so that the code will
compile successfully? (Choose all that apply)
public interface CanHop {}
public class Frog implements CanHop {
public static void main(String[] args) {
frog = new TurtleFrog();
}
}
public class BrazilianHornedFrog extends Frog {}
public class TurtleFrog extends Frog {}- AFrog
- BTurtleFrog
- CCanHop
- DObject
- EBrazilianHornedFrog
4Which statement(s) are correct about the following code? (Choose all that apply)
public class Rodent {
protected static Integer chew() throws Exception {
System.out.println("Rodent is chewing");
return 1;
}
}
public class Beaver extends Rodent {
public Number chew() throws RuntimeException {
System.out.println("Beaver is chewing on wood");
return 2;
}
}- AIt will compile without issue.
- BIt fails to compile because the type of the exception the method throws is a subclass of
the type of exception the parent method throws. - CIt fails to compile because the return types are not covariant.
- DIt fails to compile because the method is protected in the parent class and public in
the subclass. - EIt fails to compile because of a static modifier mismatch between the two methods.
5Which of the following may only be hidden and not overridden? (Choose all that apply)
- Aprivate instance methods
- Bprotected instance methods
- Cpublic instance methods
- Dstatic methods
- Epublic variables
- Fprivate variables
6Choose the correct statement about the following code:
1: interface HasExoskeleton {
2: abstract int getNumberOfSections();
3: }
4: abstract class Insect implements HasExoskeleton {
5: abstract int getNumberOfLegs();
6: }
7: public class Beetle extends Insect {
8: int getNumberOfLegs() { return 6; }
9: }- AIt compiles and runs without issue.
- BThe code will not compile because of line 2.
- CThe code will not compile because of line 4.
- DThe code will not compile because of line 7.
- EIt compiles but throws an exception at runtime.
7Which of the following statements about polymorphism are true? (Choose all that apply)
- AA reference to an object may be cast to a subclass of the object without an explicit cast.
- BIf a method takes a superclass of three objects, then any of those classes may be passed as a parameter to the method.
- CA method that takes a parameter with type java.lang.Object will take any reference.
- DAll cast exceptions can be detected at compile-time.
- EBy defining a public instance method in the superclass, you guarantee that the specific
method will be called in the parent class at runtime.
8Choose the correct statement about the following code:
1: public interface Herbivore {
2: int amount = 10;
3: public static void eatGrass();
4: public int chew() {
5: return 13;
6: }
7: }- AIt compiles and runs without issue.
- BThe code will not compile because of line 2.
- CThe code will not compile because of line 3.
- DThe code will not compile because of line 4.
- EThe code will not compile because of lines 2 and 3.
- FThe code will not compile because of lines 3 and 4.
9Choose the correct statement about the following code:
1: public interface CanFly {
2: void fly();
3: }
4: interface HasWings {
5: public abstract Object getWindSpan();
6: }
7: abstract class Falcon implements CanFly, HasWings {
8: }- AIt compiles without issue.
- BThe code will not compile because of line 2.
- CThe code will not compile because of line 4.
- DThe code will not compile because of line 5.
- EThe code will not compile because of lines 2 and 5.
- FThe code will not compile because the class Falcon doesn’t implement the interface
methods.
10Which statements are true for both abstract classes and interfaces? (Choose all that apply)
- AAll methods within them are assumed to be abstract.
- BBoth can contain public static final variables.
- CBoth can be extended using the extend keyword.
- DBoth can contain default methods.
- EBoth can contain static methods.
- FNeither can be instantiated directly.
- GBoth inherit java.lang.Object.
11What modifiers are assumed for all interface variables? (Choose all that apply)
- Apublic
- Bprotected
- Cprivate
- Dstatic
- Efinal
- Fabstract
12What is the output of the following code?
1: interface Nocturnal {
2: default boolean isBlind() { return true; }
3: }
4: public class Owl implements Nocturnal {
5: public boolean isBlind() { return false; }
6: public static void main(String[] args) {
7: Nocturnal nocturnal = (Nocturnal)new Owl();
8: System.out.println(nocturnal.isBlind());
9: }
10: }- Atrue
- BFalse
- CThe code will not compile because of line 2.
- DThe code will not compile because of line 5.
- EThe code will not compile because of line 7.
- FThe code will not compile because of line 8.
13What is the output of the following code?
1: class Arthropod
2: public void printName(double input) { System.out
.print("Arthropod"); }
3: }
4: public class Spider extends Arthropod {
5: public void printName(int input) { System.out.print("Spider"); }
6: public static void main(String[] args) {
7: Spider spider = new Spider();
8: spider.printName(4);
9: spider.printName(9.0);
10: }
11: }- ASpiderArthropod
- BArthropodSpider
- CSpiderSpider
- DArthropodArthropod
- EThe code will not compile because of line 5.
- FThe code will not compile because of line 9.
14Which statements are true about the following code? (Choose all that apply)
1: interface HasVocalCords {
2: public abstract void makeSound();
3: }
4: public interface CanBark extends HasVocalCords {
5: public- AThe CanBark interface doesn’t compile.
- BA class that implements HasVocalCords must override the makeSound() method.
- CA class that implements CanBark inherits both the makeSound() and bark() methods.
- DA class that implements CanBark only inherits the bark() method.
- EAn interface cannot extend another interface.
15Which of the following is true about a concrete subclass? (Choose all that apply)
- AA concrete subclass can be declared as abstract.
- BA concrete subclass must implement all inherited abstract methods.
- CA concrete subclass must implement all methods defined in an inherited interface.
- DA concrete subclass cannot be marked as final.
- EAbstract methods cannot be overridden by a concrete subclass.
16What is the output of the following code?
1: abstract class Reptile {
2: public final void layEggs() { System.out.println("Reptile laying eggs");
}
3: public static void main(String[] args) {
4: Reptile reptile = new Lizard();
5: reptile.layEggs();
6: }
7: }
8: public class Lizard extends Reptile {
9: public
10: }- AReptile laying eggs
- BLizard laying eggs
- CThe code will not compile because of line 4.
- DThe code will not compile because of line 5.
- EThe code will not compile because of line 9.
17What is the output of the following code?
1: public abstract class Whale {
2: public abstract void dive() {};
3: public static void main(String[] args) {
4: Whale whale = new Orca();
5: whale.dive();
6: }
7: }
8: class Orca extends Whale {
9: public void dive(int depth) { System.out.println("Orca diving"); }
10: }- AOrca diving
- BThe code will not compile because of line 2.
- CThe code will not compile because of line 8.
- DThe code will not compile because of line 9.
- EThe output cannot be determined from the code provided.
18What is the output of the following code? (Choose all that apply)
1: interface Aquatic {
2: public default int getNumberOfGills(int input) { return 2; }
3: }
4: public class ClownFish implements Aquatic {
5: public String getNumberOfGills() { return "4"; }
6: public String getNumberOfGills(int input) { return "6"; }
7: public static void main(String[] args) {
8: System.out.println(new ClownFish().getNumberOfGills(-1));
9: }
10: }- A2
- B4
- C6
- DThe code will not compile because of line 5.
- EThe code will not compile because of line 6.
- FThe code will not compile because of line 8.
19Which of the following statements can be inserted in the blank so that the code will
compile successfully? (Choose all that apply)
public class Snake {}
public class Cobra extends Snake {}
public class GardenSnake {}
public class SnakeHandler {
private Snake snake;
public void setSnake(Snake snake) { this.snake = snake; }
public static void main(String[] args) {
new SnakeHandler().setSnake( );
}
}- Anew Cobra()
- Bnew GardenSnake()
- Cnew Snake()
- Dnew Object()
- Enew String("Snake")
- Fnull
20What is the result of the following code?
1: public abstract class Bird {
2: private void fly() { System.out.println("Bird is flying"); }
3: public static void main(String[] args) {
4: Bird bird = new Pelican();
5: bird.fly();
6: }
7: }
8: class Pelican extends Bird {
9: protected void fly() { System.out.println("Pelican is flying"); }
10: }- ABird is flying
- BPelican is flying
- CThe code will not compile because of line 4.
- DThe code will not compile because of line 5.
- EThe code will not compile because of line 9.
21Which of the following statements are true? (Choose all that apply)
- ARuntime exceptions are the same thing as checked exceptions.
- BRuntime exceptions are the same thing as unchecked exceptions.
- CYou can declare only checked exceptions.
- DYou can declare only unchecked exceptions.
- EYou can handle only Exception subclasses.
22Which of the following pairs fill in the blanks to make this code compile? (Choose all that
apply)
7: public void ohNo() _____ Exception {
8: _____________ Exception();
9: }- AOn line 7, fill in throw
- BOn line 7, fill in throws
- COn line 8, fill in throw
- DOn line 8, fill in throw new
- EOn line 8, fill in throws
- FOn line 8, fill in throws new
23When are you required to use a finally block in a regular try statement (not a try-withresources)?
- ANever.
- BWhen the program code doesn’t terminate on its own.
- CWhen there are no catch blocks in a try statement.
- DWhen there is exactly one catch block in a try statement.
- EWhen there are two or more catch blocks in a try statement.
24Which exception will the following throw?
Object obj = new Integer(3);
String str = (String) obj;
System.out.println(str);- AArrayIndexOutOfBoundsException
- BClassCastException
- CIllegalArgumentException
- DNumberFormatException
- ENone of the above.
25Which of the following exceptions are thrown by the JVM? (Choose all that apply)
- AArrayIndexOutOfBoundsException
- BExceptionInInitializerError
- Cjava.io.IOException
- DNullPointerException
- ENumberFormatException
26What will happen if you add the statement System.out.println(5 / 0); to a working
main() method?- AIt will not compile.
- BIt will not run.
- CIt will run and throw an ArithmeticException.
- DIt will run and throw an IllegalArgumentException.
- ENone of the above.
27What is printed besides the stack trace caused by the NullPointerException from line 16?
1: public class DoSomething {
2: public void go() {
3: System.out.print("A");
4: try {
5: stop();
6: } catch (ArithmeticException e) {
7: System.out.print("B");
8: } finally {
9: System.out.print("C");
10: }
11: System.out.print("D");
12: }
13: public void stop() {
14: System.out.print("E");
15: Object x = null;
16: x.toString();
17: System.out.print("F");
18: }
19: public static void main(String[] args) {
20: new DoSomething().go();
21: }
22: }- AAE
- BAEBCD
- CAEC
- DAECD
- ENo output appears other than the stack trace.
28What is the output of the following snippet, assuming a and b are both 0?
3: try {
4: return a / b;
5: } catch (RuntimeException e) {
6: return -1;
7: } catch (ArithmeticException e) {
8: return 0;
9: } finally {
10: System.out.print("done");
11: }What is the output of the following snippet, assuming a and b are both 0?
3: try {
4: return a / b;
5: } catch (RuntimeException e) {
6: return -1;
7: } catch (ArithmeticException e) {
8: return 0;
9: } finally {
10: System.out.print("done");
11: }- A-1
- B0
- Cdone-1
- Ddone0
- EThe code does not compile.
- FAn uncaught exception is thrown.
29What is the output of the following program?
1: public class Laptop {
2: public void start() {
3: try {
4: System.out.print("Starting up ");
5: throw new Exception();
6: } catch (Exception e) {
7: System.out.print("Problem ");
8: System.exit(0);
9: } finally {
10: System.out.print("Shutting down ");
11: }
12: }
13: public static void main(String[] args) {
14: new Laptop().start();
15: } }- AStarting up
- BStarting up Problem
- CStarting up Problem Shutting down
- DStarting up Shutting down
- EThe code does not compile.
- FAn uncaught exception is thrown.
30What is the output of the following program?
1: public class Dog {
2: public String name;
3: public void parseName() {
4: System.out.print("1");
5: try {
6: System.out.print("2");
7: int x = Integer.parseInt(name);
8: System.out.print("3");
9: } catch (NumberFormatException e) {
10: System.out.print("4");
11: }
12: }
13: public static void main(String[] args) {
14: Dog leroy = new Dog();
15: leroy.name = "Leroy";
16: leroy.parseName();
17: System.out.print("5");
18: } }- A12
- B1234
- C1235
- D124
- E1245
- FThe code does not compile.
- GAn uncaught exception is thrown.
31What is the output of the following program?
1: public class Cat {
2: public String name;
3: public void parseName() {
4: System.out.print("1");
5: try {
6: System.out.print("2");
7: int x = Integer.parseInt(name);
8: System.out.print("3");
9: } catch (NullPointerException e) {
10: System.out.print("4");
11: }
12: System.out.print("5");
13: }
14: public static void main(String[] args) {
15: Cat leo = new Cat();
16: leo.name = "Leo";
17: leo.parseName();
18: System.out.print("6");
19: }
20: }- A12, followed by a stack trace for a NumberFormatException
- B124, followed by a stack trace for a NumberFormatException
- C12356.
- D12456
- E1256, followed by a stack trace for a NumberFormatException
- FThe code does not compile.
- GAn uncaught exception is thrown.
32What is printed by the following? (Choose all that apply)
1: public class Mouse {
2: public String name;
3: public void run() {
4: System.out.print("1");
5: try {
6: System.out.print("2");
7: name.toString();
8: System.out.print("3");
9: } catch (NullPointerException e) {
10: System.out.print("4");
11: throw e;
12: }
13: System.out.print("5");
14: }
15: public static void main(String[] args) {
16: Mouse jerry = new Mouse();
17: jerry.run();
18: System.out.print("6");
19: } }- A1
- B2
- C3
- D4
- E5
- F6
- GThe stack trace for a NullPointerException
33Which of the following statements are true? (Choose all that apply)
- AYou can declare a method with Exception as the return type.
- BYou can declare any subclass of Error in the throws part of a method declaration.
- CYou can declare any subclass of Exception in the throws part of a method declaration.
- DYou can declare any subclass of Object in the throws part of a method declaration.
- EYou can declare any subclass of RuntimeException in the throws part of a method declaration.
34Which of the following can be inserted on line 8 to make this code compile? (Choose all
that apply)
7: public void ohNo() throws IOException {
8: // INSERT CODE HERE
9: }- ASystem.out.println("it's ok");
- Bthrow new Exception();
- Cthrow new IllegalArgumentException();
- Dthrow new java.io.IOException();
- Ethrow new RuntimeException();
35Which of the following are unchecked exceptions? (Choose all that apply)
- AArrayIndexOutOfBoundsException
- BIllegalArgumentException
- CIOException
- DNumberFormatException
- EAny exception that extends RuntimeException
- FAny exception that extends Exception
36Which scenario is the best use of an exception?
- AAn element is not found when searching a list.
- BAn unexpected parameter is passed into a method.
- CThe computer caught fire.
- DYou want to loop through a list.
- EYou don’t know how to code a method.
37Which of the following can be inserted into Lion to make this code compile? (Choose all
that apply)
class HasSoreThroatException extends Exception {}
class TiredException extends RuntimeException {}
interface Roar {
void roar() throws HasSoreThroatException;
}
class Lion implements Roar {// INSERT CODE HERE
}- Apublic void roar(){}
- Bpublic void roar() throws Exception{}
- Cpublic void roar() throws HasSoreThroatException{}
- Dpublic void roar() throws IllegalArgumentException{}
- Epublic void roar() throws TiredException{}
38Which of the following are true? (Choose all that apply)
- AChecked exceptions are allowed to be handled or declared.
- BChecked exceptions are required to be handled or declared.
- CErrors are allowed to be handled or declared.
- DErrors are required to be handled or declared.
- ERuntime exceptions are allowed to be handled or declared.
- FRuntime exceptions are required to be handled or declared.
39Which of the following can be inserted in the blank to make the code compile? (Choose all
that apply)
public static void main(String[] args) {
try {
System.out.println("work real hard");
} catch ( e) {
} catch (RuntimeException e) {
}
}- AException
- BIOException
- CIllegalArgumentException
- DRuntimeException
- EStackOverflowError
- FNone of the above.
40What does the output of the following contain? (Choose all that apply)
12: public static void main(String[] args) {
13: System.out.print("a");
14: try {
15: System.out.print("b");
16: throw new IllegalArgumentException();
17: } catch (IllegalArgumentException e) {
18: System.out.print("c");
19: throw new RuntimeException("1");
20: } catch (RuntimeException e) {
21: System.out.print("d");
22: throw new RuntimeException("2");
23: } finally {
24: System.out.print("e");
25: throw new RuntimeException("3");
26: }
27: }- Aabce
- Babde
- CAn exception with the message set to "1"
- DAn exception with the message set to "2"
- EAn exception with the message set to "3"
- FNothing; the code does not compile.
Information
- Description
- Quiz on Джава, created by Еркебұлан Шолпанкул on 22/12/2018.
- Author
- Еркебұлан Шолпанкул
- Language
- English
- Views
- 8
- Rating
- No ratings yet Rate this quiz