Solved

Suppose That Sock Refers to a TCP Socket Connected to Another

Question 4

Multiple Choice

Suppose that sock refers to a TCP socket connected to another machine, and in is aScanner. Which code sends the string "Who is there?" to the other machine, receives the return answer, and then prints it?


A) PrintWriter pw = new PrintWriter(sock.getOutputStream() .println("Who is there?") ) ;
InputStream instream = sock.getInputStream() ;
Scanner in = new Scanner(instream) ;
System.out.println(in.nextLine() ) ;
B) PrintWriter pw = new PrintWriter(sock.getInputStream() ) ;
pw.println("Who is there?") ;
pw.flush() ;
InputStream instream = sock.getInputStream() ;
Scanner in = new Scanner(instream) ;
System.out.println(in.nextLine() ) ;
C) PrintWriter pw = new PrintWriter(sock.getOutputStream() ) ;
pw.flush("Who is there") ;
InputStream instream = sock.getInputStream() ;
Scanner in = new Scanner(instream) ;
System.out.println(in.nextLine() ) ;
D) PrintWriter pw = new PrintWriter(sock.getOutputStream() ) ;
pw.println("Who is there?") ;
pw.flush() ;
InputStream instream = sock.getInputStream() ;
Scanner in = new Scanner(instream) ;
System.out.println(in.nextLine() ) ;

Correct Answer:

verifed

Verified

Unlock this answer now
Get Access to more Verified Answers free of charge

Related Questions