Java Kolio Klausimai
Java Kolio Klausimai
Java Kolio Klausimai
3: int x = 5;
4: while(x >= 0) {
5: int y = 3;
6: while (y > 0) {
7: if(x<2) continue;
8: x--; y--;
9: System.out.print(x*y+" ");
10: }
11: }
A. 42 31 20 12
B. 6 4 2 3 C. 8 3 0 2
2. What could be the output of the following code given that e() could be left with a blank implementation or have a
one-line implementation that throws any type of exception?
A. ab
B. abc
C. abd
D. abcd
1: interface Climb {
2: boolean isTooHigh(int height, int limit); 3:
}
4:
5: public class Climber {
6: public static void main(Strin[] args) {
7: check((h, l) -> h > l, 5);
8: }
9: private static void check(Climb climb, int height) {
10: if (climb.isTooHigh(height, "max"))
11: System.out.println("too high");
12: else
13: System.out.println("ok");
14: }
15: }
A. ok
B. too high
A. 1
B. 11
C. 111
D. 1111
E. 11111
5. Which of the following imports can be inserted to make the Robot class compile? (Choose all that apply)
import util.*;
A. 1
B. 4
C. 1 printed 4 times
D. 4 printed 3 times
E. An exception is thrown.
A. 3/7/14 11:22 AM
B. 6/7/14 11:22 AM
C. 5/10/15 11:22 AM
D. 6/10/15 11:22 AM
3: int x = 9;
4: long y = x * (long) (++x); 5:
System.out.println(y);
A. –1
B. 9
C. 81
D. 90
10. What is the result of the following code? (Choose all that apply)
A. 0
B. 1
C. 2
D. 3
E. 4
A. 48
B. 58
C. 478
D. 578
E. 78
F. An exception is thrown.
14. Determine the output of the following code when executed with the command:
A. hello world
B. world goodbye
C. null null
D. An ArrayIndexOutOfBoundsOccurs at runtime.
A. cde
B. cdef
C. def
D. defg
A. 3/7/14 11:22 AM
B. 6/7/14 11:22 AM
C. 5/10/15 11:22 AM
D. 6/10/15 11:22 AM
A. 2
B. 3 C. 5
E. An exception is thrown.
1: class Mammal {
2: public Mammal(int age) { System.out.println("1"); }
3: }
4: public class Hippopotamus extends Mammal {
5: public Hippopotamus() { System.out.println("2"); }
6: }
A. 12
B. 1 C. 2
A. 123
B. 132
C. 12
D. 13
A. abcdefghi
B. bcdefghij
C. abcdef
D. abcabcabc
26. Given the following class definitions, which method signature could appear in a subclass of Albatross? (Choose all
that apply)
D. Long fly()
A. 1 6
B. 1 8
C. 2 6
D. 2 8
E. An exception is thrown.
A. 39
B. 49
C. 410
D. 511
A. 14
B. 2
C. 24
D. 234
A. Outputs nothing
B. Outputs roar!
A. 0
B. 0 null
C. 44
D. 44 null
E. 44.0
F. 44.0 null
layEggs(int... eggs) {
c.layEggs(1, 2);
c.layEggs(3);
c.layEggs(new Integer(2));
F. An exception is thrown.
B. String[] grades; grades = new String[] {"B", "C", "F", "A", "D"};
E. String grades[]; grades = new []String {"B", "C", "F", "A", "D"};
42. Which of the following fill in the blank to print out "c"? (Choose all that apply)
A. letters.charAt(2);
B. letters.charAt(3);
C. letters.substring(2, 2);
D. letters.substring(2, 3);
E. letters.substring(3, 3);
F. letters.substring(3, 4);
44. Suppose we have the following class named ShowDate. Which one of the following statements is true?
1: import java.util.Date;
2: public class ShowDate {
3: public static void main(String [] args) {
4: Date a = new Date();
5: Date b = new Date();
6: Date c = a;
7: System.out.println(c.toString());
8: a = null;
9: c = null;
10: } }
A. The Date object from line 4 is eligible for garbage collection immediately following line 6.
B. The Date object from line 4 is eligible for garbage collection immediately following line 8.
C. The Date object from line 4 is eligible for garbage collection immediately following line 9.
45. Which can be inserted in the blank to make this code compile? (Choose all that apply)
A. java.date.*
B. java.date.LocalDate
C. java.time.*
D. java.time.LocalDate
E. java.util.*
F. java.util.LocalDate
46. Which of the following are included as a result of this code? (Choose all that apply)
3: String s = "Hello";
4: String t = new String(s);
5: if ("Hello".equal(s)) System.out.println("one");
6: if (t == s) System.out.println("two");
7: if (t.equal(s)) System.out.println("three"); 8:
if ("Hello" == s) System.out.println("four");
9: if ("Hello" == t) System.out.println("five");
A. one
B. two
C. three
D. four
E. five
50. Which of the following complete this method to print out all the arguments? (Choose all that apply)
System.out.println(args[i]);
System.out.println(args[i]);
System.out.println(args[i]);
System.out.println(args[i]);
System.out.println(arg);
F. None of the above; args is not an array so it cannot be accessed like one.
51. Suppose we have the following classes named InventoryItem and Order. Which one of the following statements
inserted at line 2 of the Order class will make the Order class compile successfully?
1: package com.abc.products;
2: public class InventoryItem { }
1: package com.abc.orders;
2: // INSERT CODE HERE
3: public class Order { 4:
InventoryItem [] items; }
A. import com.abc.products;
B. import com.abc.products.*;
D. import com.abc.products.InventoryItem.*;
A. 2018 APRIL 2
B. 2018 APRIL 30
C. 2018 MAY 2
D. 2012 APRIL 2
E. 2012 APRIL 30
F. 2021 MAY 2
A. 0 ......................................................................................................................................................................................... 1
B............................................................................................................................................................................................... 1
C............................................................................................................................................................................................... 1
D .............................................................................................................................................................................................. 1
E ............................................................................................................................................................................................... 2
A. 124
B. 234
C. 34
D. 3 and a stack trace for Exception E. 34 and a stack trace for Exception
3: int x = 10 % 2;
4: int y = 3 / 5 + ++x;
5: int z += 4 * x;
6: System.out.print(x+","+y+","+z);
A. 0,1,0
B. 1,1,4
C. 2,1,8
D. 0,2,1
A. byteint
B. intbyte
C. Tiger is Bigger
D. Lion is Bigger
E. is Bigger
60. Which of the following lambda expressions can be passed to a function of Predicate<String> type? (Choose all that
apply)
61. Given the following code, which statements can fill in the blank on line 5 of Car to have the code compile? (Choose
all that apply)
1: package my.vehicles;
2: public class Vehicle {
3: public String make;
4: protected String model;
5: private int year;
6: int mileage;
7: }
1: package my.vehicles.cars;
2: import my.vehicles.*;
3: public class Car extends Vehicle {
4: public Car() {
5: ___________________________
6: } }
A. make = "Honda";
B. model = "Pilot";
C. year = 2009;
D. mileage = 15285;
A. [iguana]
B. [iguana, ]
C. [iguana, gecko]
63. Which are true of the following code? (Choose all that apply)
1: package aquarium;
2: public class Water {
3: public String toString() { return ""; } }
1: package aquarium;
2: public class Shark {
3: static int numFins;
4: static Water water;
5: public static void Main(String[] args) {
6: String s1 = water.toString(); 7:
String s2 = numFins.toString(); } }
64. Which of the following are true of the following code? (Choose all that apply)
A. 2018 MARCH 30
B. 2018 APRIL 30
C. 2018 MAY 30
D. 2038 MARCH 30
E. 2038 APRIL 30
F. 2038 MAY 30
1: class Animal {
2: public int getAge() {return 10;}
3: }
4: class Mammal extends Animal {
5: protected int getAge(int input) {return 7;}
6: }
7: public class Sloth extends Mammal {
8: public boolean hasFur() {return true;}
9: public static void main(String[] args) {
10: Mammal sloth = new Sloth();
11: System.out.print(sloth.getAge());
12: System.out.print(sloth.getAge(2));
13: System.out.print(sloth.hasFur());
14: }
15: }
A. 7true10
B. 10true7
67. Which of the following lines do not compile? (Choose all that apply)
1: interface Swim {
2: default void swim() { }
3: }
4: class Swimmer implements Swim {
5: public void crawl() {}
6: default void butterfly(boolean fast) {}
7: private int numberStrokes() return 0;
8: secret void secretWeapon() { }
9: }
A. Line 2
B. Line 5
C. Line 6
D. Line 7
E. Line 8
A. Color:
B. Color:null
C. Color:White
70. Which of the following is a reference variable (and not a primitive)? (Choose all that apply)
A. int[] ints;
B. long[] longs;
C. String[] strings;
D. Object[] objects;
3: int x = 10;
4: switch(x % 4.) {
5: default: System.out.print("Not divisible by 4");
6: case 0: System.out.print("Divisible by 4");
7: }
A. Not divisible by 4
B. Divisible by 4
72. Which of the following can fill in the blank on line 5 to make the code compile? (Choose all that apply)
1: package x.y;
2: public class Movie {
3: private String title;
4: protected String rating;
5: int length;
6: public String description;
7: }
1: package x.y;
2: public class MovieTheater {
3: private Movie movie = new Movie();
4: public MovieTheater() {
5: _________________________
6: } }
B. movie.rating = "R";
C. movie.length = 89;
D. movie.description = "Comedy";
3: boolean x = false;
4: int z = 0;
5: do {
6: if(z>5) {z++; x = true;}
7: else z += 3;
8: } while (!(x = true));
9: System.out.print(z);
A. 8
B. 0
C. 7
D. 3
E. 6
A. true15
B. 15true
75. What is the output of the following code? (Choose all that apply)
1: class Car {
2: public int velocity = 10;
3: }
1: public class TestDrive {
2: public static void go(Car c) {
3: c.velocity += 10;
4: }
5: public static void main(String[] args) {
6: Car porsche = new Car(); 7:
go(porsche);
8:
9: Car stolen = porsche; 10:
go(stolen);
11:
12: System.out.println(porsche.velocity);
13: }
14: }
A. 0
B. 10
C. 20
D. 30
E. 40
A. abcd
B. acbd
C. bacd
D. bdac E. dbad
77. What is the result of the following code assuming garbage collection runs on line 6?
78. Which of the following are true? (Choose all that apply)
C. If statements that do not compile are removed, the output contains one.
D. If statements that do not compile are removed, the output contains two.
E. If statements that do not compile are removed, the output contains three.
F. If statements that do not compile are removed, the output contains four.
1: class Animal {
2: public Animal(int age) { System.out.println("1"); this(); }
3: public Animal() { System.out.println("2"); }
4: }
5: public class Buffalo extends Animal {
6: public Buffalo() { 7:
System.out.println("3");
8: }
9: }
A. 123
B. 213
C. 3
D. 23
E. An exception is thrown.
81. What is the result of the following code? (Choose all that apply)
A. 0
B. 1
C. 2
D. 3
E. 4
3: int x = 4;
4: if(x <= 5 ^ x == 4)
5: System.out.print("Low");
6: else if(x==4)
7: System.out.print("Match"); 8:
if(x>1) System.out.print("High");
A. Low
B. Match
C. High
D. LowHigh
E. MatchHigh
1: interface CanSwim {
2: public static int SPEED; 3:
public void swim();
4: }
5: public class MantaRay implements CanSwim {
6: public void swim() { System.out.println("MantaRay is swimming:
"+SPEED); }
7: }
B. The code will not compile because of line 2. C. The code will not compile because
of line 3.
87. Which of the following statements can be inserted in the blank so that the code will compile successfully? (Choose
all that apply)
A. new Mammal()
B. new Antelope()
C. new WalksOn4Legs()
D. (Mammal)new Object()
E. (Mammal)new String()
F. Null
A. 1
B. 4
C. 1 printed 4 times
D. 4 printed 3 times
E. An exception is thrown.
public class A {
private int i = 6;
private int j = i;
public void A() {
i = 5; }
public static void main(String[] args) {
A a = new A();
System.out.println(a.i + a.j);
} }
A. 55
B. 56
C. 66
D. 10
E. 11
F. 12
97. Which methods will compile if inserted into Joey? (Choose all that apply)
class Kangaroo {
public void hop() { }
} class Joey extends Kangaroo
{
// INSERT CODE HERE
}
E. None of these
98. Which of the following fill in the blank to remove the blank space in this String? (Choose all that apply)
A. g.trim();
B. g.substring(0, i) + g.substring(i);
C. g.substring(0, i - 1) + g.substring(i);
A. mashyxybe
B. mashybexy
C. mayshbxye
D. mayshxybe
E. An exception is thrown.
A. Color:
B. Color:null
C. Color:White
B. Congratulations
B. 9
C. 11
D. 13
F. An exception is thrown.
B. true
F. An exception is thrown.
public class A {
private int i = 6;
private int j = i;
public A() { i =
5; }
public static void main(String[] args) {
A a = new A();
System.out.println(a.i + a.j);
} }
A. 55
B. 56
C. 66
D. 10
E. 11 F. 12
B. 23
E. 234
110. Which of braces can be removed without causing a compiler error? (Choose all that apply)
A. Duck Goose
B. Duck ArrayIndexOutOfBoundsException
C. Goose
D. Goose ArrayIndexOutOfBoundsException
1: interface Climb {
2: boolean isTooHigh(int height, int limit); 3:
}
4:
5: public class Climber {
6: public static void main(String[] args) {
7: check((h, l) -> h > l, 5);
8: }
9: private static void check(Climb climb, int height) {
10: if (climb.isTooHigh(height, 10))
11: System.out.println("too high");
12: else
13: System.out.println("ok");
14: }
15: }
A. ok
B. too high
A. BigCat purrs!
B. Ocelot purrs!
A. onetwothree
B. onetwothree2
C. one0two1three2
1: public class C {
2: String seq = "c";
3: { seq += "g"; }
4: public C() {
5: this("abc");
6: seq += "y";
7: }
8: public C(String s) {
9: seq += "e";
10: }
11: { seq += "z"; }
12: public static void main(String[] args) {
13: C c = new C();
14: System.out.println(c.seq);
15: } }
A. ceygz
B. cgeyz
C. cgyez
D. cgzey
E. cgzye F. cyegz
A. 14
B. 2
C. 24
D. 3
E. 34
3: int x = 10, y = 3;
4: if(x % y == 2)
5: System.out.print("two");
6: System.out.print(x%y);
7: if(x % y == 1) System.out.print("one");
A. two
B. two1
C. two2
D. one
E. 1one