Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Test your skills through the online practice test: Core Java Quiz Online Practice Test

Ques 56. Explain the usage of the keyword transient?

This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).
For example:
class T { transient int a; // will not persist int b; // will persist }

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class Logon implements Serializable {
	private Date date = new Date();
	private String username;
	private transient String password;
	
	public Logon(String name, String pwd) {
		username = name;
		password = pwd;
	}
	public String toString() {
		String pwd = (password == null) ? "(n/a)" : password;
		return "logon info: n username: " + username + "n date: " + date
		+ "n password: " + pwd;
	}

	public static void main(String[] args) throws Exception {
		Logon a = new Logon("Hulk", "myLittlePony");
		System.out.println("logon a = " + a);
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
		"Logon.out"));
		o.writeObject(a);
		o.close();
		Thread.sleep(1000); // Delay for 1 second
		// Now get them back:
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
		"Logon.out"));
		System.out.println("Recovering object at " + new Date());
		a = (Logon) in.readObject();
		System.out.println("logon a = " + a);
	}
}

Is it helpful? Add Comment View Comments
 

Ques 57. What is the difference between constructors and other methods in core java?

  • Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.
  • Constructor needs to have the same name as that of the class whereas functions need not be the same.
  • There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value.
  • There is no return statement in the body of the constructor.
  • The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

Is it helpful? Add Comment View Comments
 

Ques 58. What is the range of the char type?

The range of the char type is 0 to 2^16 - 1.

Is it helpful? Add Comment View Comments
 

Ques 59. What is the difference between a while statement and a do statement?

A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

Is it helpful? Add Comment View Comments
 

Ques 60. What are the legal operands of the instanceof operator?

The left operand is an object reference or null value and the right operand is a class, interface, or array type.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook