MJT2019 c2 v1 With Answers PDF
MJT2019 c2 v1 With Answers PDF
#:
b. { "name": "Smiley",
"age": 20,
"phone": null,
"email": null,
"happy": true }
c. { "name": "Smiley",
"age": 20,
"phone": { "888-123-4567", "888-765-4321" },
"email": "[email protected]",
"happy": true }
Gson gson = new Gson();
String jsonString = gson.toJson(song, Song.class);
4. REST is?
a. Architectural style
b. Protocol
c. Translated as "Ailyak" in Plovdiv
d. Web framework
7. What does the following code fragment do? Comment the semantics of each
line 1-6
9. Which method of the Buffer class would you use to prepare the buffer for
reading?
a. prepare()
b. clear()
c. flip()
d. read()
10. Write a java code snippet that will allow your application to listen on
port 4444 for incoming connections and to accept them. Use classes from
package java.net.
ServerSocket serverSocket = new ServerSocket(4444);
Socket socket = serverSocket.accept();
11. In the code below, there is one line missing. Find which line is missing
and where it should be entered. If you do not fix it, the code will enter
an endless loop.
while (true) {
int readyChannels = selector.select();
if (readyChannels == 0) continue;
12. Are there any bugs in the code fragment below? If yes, mark and fix them.
13. How can a thread exit from a deadlock with another thread?
a) by calling its own yield() method
b) by calling the other thread’s yield method
c) it cannot exit a deadlock once has occurred
d) by calling Thread.sleep()
15. Which line(s) of code inserted at the place of the comment will start a
thread?
16. What are some benefits of using thread executors instead of creating and
starting threads manually?
17. How many threads will be started during the whole lifecycle of the
following program?
11
18. Given the following, find the longest word of the list by using the Java
Stream API:
List<String> list = Arrays.asList("The River", "Gravity", "I Found");
list.stream().max(Comparator.comparingInt(String::length)).get();
19. In the context of functional programming what is a “side effect” and what
is the term for a function that does not cause side effects?
21. Create 2 JUnit tests for the class below using Mockito or stubs. Write
just the test methods.
@Test(expected = UserAlreadyExistsException.class)
public void testRegisterThrowsAppropriateException() {
UserRepository mock = mock(UserRepository.class);
when(mock.exists("[email protected]")).thenReturn(true);
@Test
public void testRegisterSavesUser() {
UserRepository mock = mock(UserRepository.class);
when(mock.exists("[email protected]")).thenReturn(false);
assertEquals(actual.getEmail(), "[email protected]");
assertEquals(actual.getPassword(), "weak");
}
22. What is the difference between stubbing and mocking? In which cases
mocking is preferred?
import java.io.FileNotFoundException;
import java.io.IOException;
26. What is the fastest way to get all the unique elements from an array
using data structures in Java? Write a code snippet.
Set<T> mySet = new HashSet<T>(Arrays.asList(someArray));
Set<T> mySet = Set.of(someArray);
var mySet = Set.of(someArray);
What is the most suitable data structure if we want to keep books sorted
by name?
What else should be done for our solution to work?
28. Given:
public class MyKeys {
Integer key;
MyKeys(Integer k) {
key = k;
}
@Override
public boolean equals(Object o) {
return ((MyKeys) o).key == this.key;
}
}
}
a. SOAP
b. JAX-RS
c. HTML
d. XML
e. HTTP
f. AJAX
g. CSS
h. YAML
a. BD
b. BCD
c. BDE
d. BCDE
33. Are there any bugs in the following code fragment? Identify and fix, if
any
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
1. HttpClient cannot be instantiated with new → use the static factory method or
the static builder method
2. uri() is not applicable to String, it requires URI
instance: URI.create("https://www.youtube.com/")
3. client.request instead of client.send
4. missing .body() before .length
34. List HTTP’s status code groups and their use cases.
GET / HTTP/1.1
38. What is printed to the standard output executing the following code?
Daemon thread is a low priority thread (in context of JVM) that runs in
background to perform tasks that do not prevent the JVM from exiting (even if
the daemon thread itself is running) when all the user threads (non-daemon
threads) finish their execution.
a. Vector
b. HashMap
c. ArrayList
d. Hashtable
e. BlockingQueue
f. TreeSet