andraskiss.hu

Home / Development / Software Development / Java / LinkedIn Java Assessment

LinkedIn Java Assessment answers

This page contains LinkedIn Java Assessment questions and answers from the past couple of years. The purpose of this content is to provide insight into the LinkedIn Java Assessment test and thereby facilitate your preparation and expand your knowledge.

LinkedIn

Topics: Control Flow, Core API, Functional Programming, Fundamentals, Object-Oriented Programming.

  1. Question:

    What function could you use to replace slashes for dashes in a list of dates?

    List dates = new ArrayList();
    



    Answer:
    UnaryOperator replaceSlashes = date -> date.replace("/", "-");

  2. Question:

    What is the output of this code?

    import java.util.*;
    
    class Main{
    	public static void main(String[] args){
    		String[] array = new String[]{"A", "B", "C"};
    		List list1 = Arrays.asList(array);
    		List list2 = new ArrayList<>(Arrays.asList(array));
    		List list3 = new ArrayList<>(Arrays.asList("A", new String("B"), "C"));
    		System.out.print(list1.equals(list2));
    		System.out.print(list1.equals(list3));
    	}
    }



    Answer:
    truetrue

  3. Question:

    Which is the correct return type for the processFunction method?

    ___ processFunction(Integer number, Function lambda){
    	return lambda.apply(number);
    }



    Answer:
    String

  4. Question:

    This code does not compile. What needs to be changed so that it does?

    public enum Direction{
    	EAST("E"), WEST("W"), NORTH("N"), SOUTH("S");
    	private final String shortCode;
    	
    	public String getShortCode(){
    		return shortCode;
    	}
    }



    Answer:
    Add a constructor that accepts a String parameter and assigns it to the field shortCode.

  5. Question:

    What will be printed?

    public class Berries{
    	String berry = "blue";
    	
    	public static void main(String[] args){
    		new Berries().juicy("straw");
    	}
    	
    	void juicy(String berry){
    		this.berry = "rasp";
    		System.out.println(berry + "berry");
    	}
    }



    Answer:
    strawberry

  6. Question:

    What variable type should be declared for capitalize?

    List songTitles = Arrays.asList("humble", "element", "dna");
    ___ capitalize = str -> str.toUpperCase();
    songTitles.stream().map(capitalize).forEach(System.out::println);
    



    Answer:
    Function

  7. Question:

    Which keyword would not be allowed here?

    class Unicorn{
    	___ Unicorn(){
    	}
    }
    



    Answer:
    static

  8. Question:

    Which language feature ensures that objects implementing the AutoClosable interface are closed when it completes?



    Answer:
    try-with-resources

  9. Question:

    How do you write a foreach loop that will iterate over ArrayList pencilCase?



    Answer:
    for (Pencil pencil : pencilCase){}

  10. Question:

    Is this an example of method overloading or overriding?

    class Car{
    	public void accelerate(){
    	}
    }
    
    class Lambo extends Car{
    	public void accelerate(int speedLimit){
    	}
    	
    	public void accelerate(){
    	}
    }
    



    Answer:
    both

  11. Question:

    What is the problem with this code?

    class Main{
    	public static void main(String[] args){
    		List list = new ArrayList(Arrays.asList("a", "b", "c"));
    		
    		for(String value : list){
    			if(value.equals("a")){
    				list.remove(value);
    			}
    		}
    		
    		System.out.println(list);	//outputs [b, c]
    	}
    }
    



    Answer:
    Modifying a collection while iterating through it can throw a ConcurrentModificationException.

  12. Question:

    What is the result of this code?

    int a = 1;
    int b = 0;
    int c = a/b;
    System.out.print(c);
    



    Answer:
    It will throw an ArithmeticException.

  13. Question:

    What does this code print?

    String a = "bikini";
    String b = new String("bikini");
    String c = new String("bikini");
    System.out.println(a == b);
    System.out.println(b == c);
    



    Answer:
    false
    false

  14. Question:

    What does this code print?

    public static void main(String[] args){
    	int x = 5, y = 10;
    	swapsies(x, y);
    	System.out.println(x + " " + y);
    }
    
    static void swapsies(int a, int b){
    	int temp = a;
    	a = b;
    	b = temp;
    }
    



    Answer:
    5 10

  15. Question:

    What is the value of myCharacter after line 3 is run?

    public class Main{
    	public static void main(String[] args){
    		char myCharacter = "piper".charAt(3);
    	}
    }
    



    Answer:
    e

  16. Question:

    What is the output of this code?

    class Main{
    	static int count = 0;
    	
    	public static void main(String[] args){
    		if(count < 3){
    			count++;
    			main(null);
    		}else{
    			return;
    		}
    		System.out.println("Hello World!");
    	}
    }
    



    Answer:
    It will print "Hello World!" three times.

  17. Question:

    What phrase indicates that a function receives a copy of each argument passed to it rather than a reference to the objects themselves?



    Answer:
    Pass by value

  18. Question:

    What will this program print out to the console when executed?

    import java.util.LinkedList;
    
    public class Main{
    	public static void main(String[] args){
    		LinkedList list = new LinkedList<>();
    		list.add(5);
    		list.add(1);
    		list.add(10);
    		System.out.println(list);
    	}
    }
    



    Answer:
    [5, 1, 10]

  19. Question:

    By implementing encapsulation, you cannot directly access the class's ___ properties unless you are writing code inside the class itself.



    Answer:
    private

  20. Question:

    In Java, what is the scope of a method's argument or parameter?
    - outside the method
    - neither inside nor outside the method
    - both inside and outside the method
    - inside the method



    Answer:
    inside the method

  21. Question:

    When should you use a static method?
    - when your method is dependent on the specific instance that calls it
    - when your method is related to the object's characteristics
    - when you want your method to be available independently of class instances
    - when your method uses an object's instance variable




    Answer:
    When you want your method to be available independently of class instances.

  22. Question:

    What is the output of this code?

    import java.util.*
    
    class Main{
    	public static void main(String[] args){
    		String[] array = {"abc", "2", "10", "0"};
    		List list = Arrays.asList(array);
    		Collections.sort(list);
    		System.out.println(Arrays.toString(array));
    	}
    }
    



    Answer:
    [0, 10, 2, abc]

  23. Question:

    What is the result of this code?

    try{
    	System.out.print("Hello World");
    }catch(Exception e){
    	System.out.print("e");
    }catch(ArithmeticException e){
    	System.out.print("e");
    }finally{
    	System.out.print("!");
    }
    



    Answer:
    It will not compile because the second catch statement is unreachable.

  24. Question:

    Which code snippet is valid?
    - ArrayList words = {"Hello", "World"};
    - ArrayList words = new ArrayList<>(Arrays.asList("Hello", "World"));
    - ArrayList words = new ArrayList(){"Hello", "World"};
    - ArrayList words = Arrays.asList("Hello", "World");



    Answer:
    ArrayList words = new ArrayList<>(Arrays.asList("Hello", "World"));

  25. Question:

    What method should be added to the Duck class to print the name Moby?

    public class Duck{
    	private String name;
    	
    	Duck(String name){
    		this.name = name;
    	}
    	
    	public static void main(String[] args){
    		System.out.println(new Duck("Moby"));
    	}
    }
    



    Answer:
    public void toString(){
    	System.out.println(this.name);
    }
    

  26. Question:

    Which functional interfaces does Java provide to serve as data types for lambda expressions?



    Answer:
    Observer, Observable

  27. Question:

    What code would you use to tell if "schwifty" is of type String?



    Answer:
    "schwifty" instanceof String

  28. Question:

    What can you use to create new instances in Java?
    - another instance
    - field
    - constructor
    - private method



    Answer:
    constructor

  29. Question:

    Given the string "strawberries" saved in a variable called fruit, what would fruit.substring(2, 5) return?



    Answer:
    raw

  30. Question:

    You have an ArrayList of names that you want to sort alphabetically. Which approach would NOT work?
    - names.stream().sorted((s1, s2) -> s1.compareTo(s2)).collect(Collectors.toList())
    - Collections.sort(names)
    - names.sort(List.Descending)
    - names.sort(Comparator.comparing(String::toString))



    Answer:
    names.sort(List.DESCENDING)

  31. Question:

    What is the output of this code?

    class Main{
    	public static void main(String[] args){
    		int array[] = {1, 2, 3, 4};
    		
    		for(int i = 0; i < array.size(); i++){
    			System.out.print(array[i]);
    		}
    	}
    }
    



    Answer:
    It will not compile because of line 4.

  32. Question:

    The runtime system starts your program by calling which function first?



    Answer:
    main

  33. Question:

    What is the result of this code?

    class Main{
    	public static void main(String[] args){
    		System.out.println(print(1));
    	}
    	
    	static Exception print(int i){
    		if(i > 0){
    			return new Exception();
    		}else{
    			throw new RuntimeException();
    		}
    	}
    }
    



    Answer:
    "java.lang.Exception"

  34. Question:

    What is the output of this code?

    class{
    	public static void main(String[] args){
    		List list = new ArrayList();
    		list.add("hello");
    		list.add(2);
    		System.out.print(list.get(0) instanceof Object);
    		System.out.print(list.get(1) instanceof Integer);
    	}
    }
    



    Answer:
    truetrue

  35. Question:

    What is the output of this code?

    class Main{
    	public static void main(String[] args){
    		String message = "Hello world!";
    		String newMessage = message.substring(6, 12) + message.substring(12, 6);
    		System.out.println(newMessage);
    	}
    }
    



    Answer:
    "world!!world"

  36. Question:

    Which letters will print when this code is run?

    public static void main(String[] args){
    	try{
    		System.out.println("A");
    		badMethod();
    		System.out.println("B");
    	}catch(Exception ex){
    		System.out.println("C");
    	}finally{
    		System.out.println("D");
    	}
    }
    
    public static void badMethod(){
    	throw new Error();
    }
    



    Answer:
    A and D

  37. Question:

    How many times will this code print "Hello World!"?

    class Main{
    	public static void main(String[] args){
    		for(int i = 0; i < 10; i = i++){
    			i += 1;
    			System.out.println("Hello World!");
    		}
    	}
    }
    



    Answer:
    5 times

  38. Question:

    What is the output of this code?

    class Main{
    	public static void main(String[] args){
    		String message = "Hello";
    		
    		for(int i = 0; i < message.length(); i++){
    			System.out.print(message.charAt(i + 1));
    		}
    	}
    }
    



    Answer:
    "ello"

  39. Question:

    Which is the most up-to-date way to instantiate the current date?
    - Calendar.getInstance().getTime()
    - new Date(System.currentTimeMillis())
    - new SimpleDateFormat("yyyy-MM-dd").format(new Date())
    - LocalDate.now()



    Answer:
    new Date(System.currentTimeMillis())

  40. Question:

    What does this code print?

    System.out.print("apple".compareTo("banana"));



    Answer:
    a negative number

  41. Question:

    What is the output of this code?

    class Main{
    	public static void main(String[] args){
    		int a = 123451234512345;
    		System.out.println(a);
    	}
    }



    Answer:
    Nothing - this will not compile

  42. Question:

    Which statement is NOT true?
    - An anonymous class may specify an interface as its base type.
    - An anonymous class does not require a zero-argument constructor.
    - An anonymous class may specify an abstract base class as its base type.
    - An anonymous class may specify both an abstract class and interface as base types.



    Answer:
    - An anonymous class does not require a zero-argument constructor.

  43. Question:

    How can you achieve runtime polymorphism in Java?
    - Method overriding
    - Method overrunning
    - Method calling
    - Method overloading



    Answer:
    - Method overriding.

  44. Question:

    Add a Duck called "Waddles" to the ArrayList ducks.

    public class Duck{
    	private String name;
    	
    	Duck(String name){
    	}
    }
    



    Answer:
    ducks.add(new Duck("Waddles"));

  45. Question:

    Which is an INVALID way to iterate over List theList?

    -

    for(int i = 0; i < theList.size(); i++){
    	System.out.println(theList.get(i));
    }

    -
    Iterator it = theList.iterator();
    for(it.hasNext()){
    	System.out.println(it.next());
    }

    -
    for(Object object : theList){
    	System.out.println(object);
    }

    -
    theList.forEach(System.out::println);



    Answer:
    theList.forEach(System.out::println);

  46. Question:

    What is the output of this code?

    import java.util.*;
    
    class Main{
    	public static void main(String[] args){
    		List list = new ArrayList<>();
    		list.add(true);
    		list.add(Boolean.parseBoolean("FalSe"));
    		list.add(Boolean.TRUE);
    		System.out.print(list.size());
    		System.out.print(list.get(1) instanceof Boolean);
    	}
    }
    



    Answer:
    3true

  47. Question:

    What statement returns true if "nifty" is of type String?
    -

    "nifty".getType() == String

    -
    "nifty" instanceof String

    -
    "nifty".getType().equals("String")

    -
    "nifty".getClass().getSimpleName() == "String"




    Answer:
    "nifty" instanceof String

  48. Question:

    Which is the most reliable expression for testing whether the values of two string variables are the same?
    -

    string1.matches(string2)

    -
    string1.equals(string2)

    -
    string == string2

    -
    string = string2




    Answer:
    string1.equals(string2)

  49. Question:

    What is the result of this code?

    class Main{
    	Object message(){
    		return "Hello!";
    	}
    	public static void main(String[] args){
    		System.out.print(new Main().message());
    		System.out.print(new Main2().message());
    	}
    }
    class Main2 extends Main{
    	String message(){
    		return "World!";
    	}
    }
    



    Answer:
    Hello!World!

  50. Question:

    Declare a variable that holds the first four digits of PI.
    -

    int pi = 3.141;

    -
    float pi = 3.141;

    -
    decimal pi = 3.141;

    -
    double pi = 3.141;




    Answer:
    double pi = 3.141;

  51. Question:

    Given this class, how would you make the code compile?

    public class TheClass{
    	private final int x;
    }
    

    -
    public TheClass(){
    	x = 77;
    }
    

    -
    private void setX(int x){
    	this.x = x;
    }
    public TheClass(){
    	setX(77);
    }
    

    -
    public TheClass(){
    	x += 77;
    }
    

    -
    public TheClass(){
    	x = null;
    }
    




    Answer:
    private void setX(int x){
    	this.x = x;
    }
    public TheClass(){
    	setX(77);
    }

  52. Question:

    You have a variable of named employees of type List containing multiple entries. The Employee type has a method getName() that returns the employee name. Which statement property extracts a list of employee names?
    -

    employees.filter(Employee::getName).collect(Collectors.toUnmodifiableList());

    -
    employees.stream().map(Employee::getName).collect(Collectors.toList());

    -
    employees.collect(employee -> employee.getName());

    -
    employees.stream().collect((e) -> e.getName());




    Answer:
    -
    employees.stream().map(Employee::getName).collect(Collectors.toList());

  53. Question:

    You have an instance of type Map named instruments containing the following key-value pairs: guitar=1200, cello=3000, and drum=2000. If you add the new key-value pair cello=4500 to the Map using the put method, how many elements do you have in the Map when you call instruments.size()?
    - 3
    - 2
    - 4
    - When calling the put method, Java will throw an exception.



    Answer:
    3

  54. Question:

    You get a NullPointerException. What is the most likely cause?
    - A file that needs to be opened cannot be found.
    - The object you are using has not been instantiated.
    - Your code has used up all available memory.
    - A network connection has been lost in the middle of communications.



    Answer:
    The object you are using has not been instantiated.

  55. Question:

    Which statement about constructors is not true?
    - A constructor does not define a return value.
    - A class can have multiple constructors with a different parameter list.
    - You can call another constructor with 'this' or 'super'.
    - Every class must explicitly define a constructor without parameters.



    Answer:
    Every class must explicitly define a constructor without parameters.

  56. Question:

    Which statement must be inserted on line 1 to print the value true?

    1:
    2:

    Optional opt = Optional.of(val);

    3:
    System.out.println(opt.isPresent());


    -
    String val = null;

    -
    Integer val = 15;

    -
    String val = "Sam";

    -
    Optional val = Optional.empty();




    Answer:
    String val = "Sam";

  57. Question:

    Which class does not implement the java.util.Collection interface?
    -

    java.util.HashMap

    -
    java.util.Vector

    -
    java.util.HashSet

    -
    java.util.ArrayList




    Answer:
    java.util.HashMap

  58. Question:

    Which interface definition allows this code to compile?

    int length = 5;
    Square square = x -> x*x;
    int a = square.calculate(length);

    -
    @FunctionalInterface
    public interface Square{
    	void calculate(int x, int y);
    }

    -
    @FunctionalInterface
    public interface Square{
    	int calculate(int... x);
    }

    -
    @FunctionalInterface
    public interface Square{
    	int calculate(int x);
    }

    -
    @FunctionalInterface
    public interface Square{
    	void calculate(int x);
    }




    Answer:
    @FunctionalInterface
    public interface Square{
    	int calculate(int x);
    }

  59. Question:

    How do you create and run a new Thread for this class?

    import java.util.Date;
    
    public class CurrentDateRunnable implements Runnable{
    	@Override
    	public void run(){
    		while(true){
    			System.out.println("Current date: " + new Date());
    			
    			try{
    				Thread.sleep(5000);
    			}catch(InterruptedException e){
    				throw new RuntimeException(e);
    			}
    		}
    	}
    }
    


    -
    new CurrentDateRunnable().run();

    -
    new CurrentDateRunnable().start();

    -
    Thread thread = new Thread(new CurrentDateRunnable());
    thread.start();

    -
    new Thread(new CurrentDateRunnable()).join();




    Answer:
    Thread thread = new Thread(new CurrentDateRunnable());
    thread.start();

  60. Question:

    What kind of relationship does extends denote?
    - is-a
    - was-a
    - has-a
    - uses-a



    Answer:
    is-a

  61. Question:

    You have a list of Bunny objects that you want to sort by weight using Collections.sort. What modification would you make to the Bunny class?
    - Implement Sortable and override the sortBy method.
    - Override the equals method inside the Bunny class.
    - Implement the Comparable interface by overriding the compareTo method.
    - Add the keyword default to the weight variable.



    Answer:
    Implement the Comparable interface by overriding the compareTo method.

  62. Question:

    What is displayed when this code is compiled and executed?

    public class Main{
    	public static void main(String[] args){
    		int x = 5;
    		x = 10;
    		System.out.println(x);
    	}
    }
    




    Answer:
    10