1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Tresset [83]
4 years ago
8

Given the following piece of code: class Student { public void talk(){} } public class Test{ public static void main(String args

[]){ Student t = null; try { t.talk(); } catch(NullPointerException e){ System.out.print("There is a NullPointerException. "); } catch(Exception e){ System.out.print("There is an Exception. "); } System.out.print("Everything ran fine. "); } } what will be the result?
Computers and Technology
1 answer:
Mariulka [41]4 years ago
3 0

Answer:

In the output of the given piece of code, the following lines are printed on the output screen:

There is a NullPointerException. Everything ran fine.    

Explanation:

In the given code

class Student {

   public void talk(){} }

This is a Student class which has a function talk()

The following is the Test class

public class Test{

   public static void main(String args[])

   { Student t = null; try { t.talk(); }

   catch(NullPointerException e)

   { System.out.print("There is a NullPointerException. ");        

   } catch(Exception e)

   { System.out.print("There is an Exception. "); }

   System.out.print("Everything ran fine. "); } }

The above chunk of code creates a Student class object named t which is set to null. A null value is assigned to "t" to indicate that it points to nothing.

In the program a method talk() is called on a null reference. This results in NullPointerException. This is a run time exception. Since no object is created so compiler will not identify this error and this error comes in run time.

The try statement defines a code chunk that is to be tested in case of errors when the code chunk is execute. This is basically used for exception or error handling. The catch statement defines a code chunk (one or two lines of code) that executes if an error occurs in try part (block of code).

Sometimes it is needed to create a reference of the object even before creating an object and this has to be initialized or assigned a value. So null is assigned to such an object reference.

If the talk() method is intended to do something to the object t, which points to nothing, then try throws NullPointerException to indicate this error.

Since the program attempts to use an object reference that contains a null value , so when the program is executed, the try part throws NullPointerException and catch part is executed to indicate this exception. The catch part that executes contains the following statement:

   { System.out.print("There is a NullPointerException. ");

So this message is printed at output screen followed by this statement  System.out.print("Everything ran fine. "); } } message. The second catch will not execute as there is no other error in the program except for the NullPointerException. So the output is:

There is a NullPointerException. Everything ran fine.  

You might be interested in
Which language do you use to add functionality to a web page
Dovator [93]
The answer is Javascript. In partner with HTML/HTML5 (Hypertext markup language : which provides the structure and CSS (Client Side Scripting : Provides the design to the structure). JS or Javascript programming language provides the function at the backend of a webpage. It responds to the input and also calls on the PHP / SQL Scripts to tap the database.
6 0
3 years ago
Which of the following combinations of keys is used as a short for saving a document on a computer
Masja [62]

ctrl+s  

is used to save

Hope this helped

-scav

7 0
3 years ago
List three reasons you might use hwinfo when troubleshooting and upgrading a computer
Elza [17]

Solution:

three reasons you might use hwinfo when troubleshooting and upgrading a computer are as follows:

1) If we want to identify a hardware component with out opening the case.

2. if we want identify Features of a motherboard, video card, or processor.

3. establish benchmarks for the components in

8 0
4 years ago
You are troubleshooting a connectivity problem in your corporate network and want to isolate the problem. You suspect that a rou
FromTheMoon [43]

Answer:

Something

Explanation:

Idk

3 0
4 years ago
_____ consists of computer equipment used to perform input, processing, and output activities.
goldfiish [28.3K]
<span> (keyboards, mice, scanning, computer chips)</span>
6 0
3 years ago
Other questions:
  • A one page document that introduces you, your skills and background and asks for an interview is an example of what type of docu
    5·2 answers
  • Given two variables firstInClass and secondInClass which have already been associated with values, write code which swaps the va
    9·1 answer
  • Steven, Marcos, and Juan are having a free-throw shooting contest. Each boy
    9·1 answer
  • For each of the descriptions below, perform the following tasks:
    11·1 answer
  • Which of these can be considered data ?
    6·2 answers
  • What are the main dimensions of information systems
    8·1 answer
  • Why is it important to install updates? Why should you use the Power button on the Start menu instead of the Power button on you
    7·1 answer
  • The Open Systems Interconnection (OSI) Reference Model: defines standards for many aspects of computing and communications withi
    12·1 answer
  • 2. 25 POINTS!!!!!!!!!
    8·2 answers
  • g Deliverables: 1.Referencing the Arduino attachInterrupt() documentationand the provided code, what does the attachIntterupt()
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!