Tuesday, May 11, 2021

Java String Interview Questions and Answers

1) What is String in Java? Is String is data type?
String in Java is not a primitive data type like int, long or double. The string is a class or in more simple term a user-defined type. This is confusing for someone who comes from a C background. The string is defined in java.lang package and wrappers its content in a character array. String provides equals() method to compare two String and provides various other methods to operate on String like toUpperCase() to convert String into upper case, replace() to replace String contentssubstring() to get substring, split() to split long String into multiple String.

Java string interview questions

2) Why is String final in Java?
The string is final by design in Java, some of the points which make sense why String is final is Security, optimization and to maintain a pool of String in Java. 

3) What is the difference between String and StringBuffer in Java?
This is probably the most common question on String I have seen in Java interviews. Though String and StringBuffer are two different class they are used in the context of concatenating two Strings, Since String is immutable in Java every operation which changes String produces new String, which can be avoided by using StringBuffer.

4) What is the difference in String on C and Java?
If you have mentioned C in your resume, then you are likely to face this String interview question. Well, C String and Java String are completely different from each other, C String is a null-terminated character array while String in Java is an Object. Also, String is more feature rich in Java than C.

5) Why char array is better than String for storing password?
This String interview question is debatable and you might not agree with interviewer but this is also a chance to show that how deep and differently you can think of. One of the reasons which people give Why you should store a password in char array over String is related to immutability since it's not possible to erase contents of String but you can erase contents of a char array. See Why char array preferred over String for a password for a complete discussion.

6) How do you compare two String in Java?
This is another common String interview question which appears on fresher level interviews. There are multiple ways to compare two String like equals() method, equalsIgnoreCase() etc, You can also see 4 ways to compare String in Java for more examples. The main thing which interviewer checks is that whether candidate mentioned equality operator or not "==", comparing String with equality operator is a common mistake which works in some case and doesn't work in other. next String interview question is follow-up up of this.

7) Can we compare String using == operator? What is the risk?
As discussed in previous String question, You can compare String using equality operator but that is not suggested or advised because equality operator is used to compare primitives and equals() method should be used to compare objects. As we have seen in the pitfall of autoboxing in Java that how equality operator can cause a subtle issue while comparing primitive to Object, anyway String is free from that issue because it doesn't have a corresponding primitive type and not participate in autoboxing.
Almost all the time comparing String means comparing contents of String i.e. characters and equals() method is used to perform character-based comparison. equals() return true if two String points to the same object or two String has same contents while == operator returns true if two String object points to the same object but return false if two different String object contains same contents. That explains why sometimes it works and sometimes it doesn't.

8) How does substring method work in Java?
This is one of the tricky Java question relate to String and until you are familiar with the internals of String class, it's difficult to answer. Substring shares same character array as original String which can create a memory leak if original String is quite big and not required to retain in memory but unintentionally retained by substring which is very small in size and prevents large array from begin claimed during Garbage collection in Java.

Java string interview questions

9) What is String pool in Java?
Another tough Java question asked in String interview. String pool is a special storage area in Java heap, mostly located on PerGen space, to store String literals like "ABC". When Java program creates a new String using String literal, JVM checks for that String in the pool and if String literal is already present in the pool than the same object is returned instead of creating a whole new object. String pool check is only performed when you create String as literal, if you create String using new() operator, a new String object will be created even if String with the same content is available in the pool.

10) What does intern() method do in Java?
As discussed in previous String interview question, String object created by new() operator is by default not added in String pool as opposed to String literal. The intern method allows putting a String object into a pool.

11) Is string thread-safe in Java?
If you are familiar with the concept of immutability and thread-safety you can easily answer this String interview question in Java. Since String is immutable, it is thread-safe and it can be shared between multiple threads without external synchronization.

If you are seriously preparing for Java interviews and do not want to leave any stone unturned, I strongly suggest you go through questions given in Java Programming Interview Exposed, one of the rare book which covers all important topics for Java interviews.
String-based Coding Questions
These questions are mostly based upon Java's implementation of String and you can only answer them well if you have good knowledge of java.lang.String class. But, String is a very general data structure and you will find it in almost all programming and script language e.g. C, C++, C#, Python, Perl or Ruby. That's why I am going to share some more String based coding question, which is not Java specific. You can solve these question in any programming language as they are mostly logic based programming question.
Thrift Savings Plan (TSP) is the federal government's retirement savings and investment plan and offers many of the same types of savings and tax benefits as 401(k) plans used by private employers. TSP is administered by the Federal Retirement Thrift Investment Board and is the largest defined contribution plan in the world. As a defined contribution plan, the retirement income you receive from TSP depends on the contributions made during your working years.

Java string interview questions


1) Write a Java program to reverse String in Java without using any API?
This means you can not use StringBuffer's reverse() method or any of String utility method, all you can have is a character array for reversing contents.

2) Write a Program to check if a String is a palindrome or not? 
For example, a String e.g. "madam" is a palindrome but "book" is not a palindrome. You also need to solve this question without taking any help from Java String API.

3) Write a Java program to check if two String are Anagram or not? 
You need to write method e.g. isAnagram(String first, String second) which will return true if second String is an anagram of the first string. An anagram must contain the same number of characters and exactly the same characters but in different order e.g. top and pot, or army and mary.

4) Write a method in Java to remove any character from String?
For example, you need to write method remove(String word, char remove this), this method should return a String without character, which is asked to remove. you can use indexOf()substring() and similar methods from String class, but your method must handle corner cases e.g. passing null or empty String, String containing just one character etc.

5) Write a method to split a comma separated String in Java? 

6) Write Java program to print all permutations of a String e.g. passing "ABC" will print all permutations like "BCA", "CBA" etc

If you are hungry for more String based coding question, you can also check the Cracking the Coding Interview book, a collection of 189 programming questions and solutions from various programming job interviews of reputed tech companies like Amazon, Google, Facebook, and Microsoft.
See also: MySainsburys is the name of the official Sainsburys employee website. You will need to access the official Oursainsburys login page and about payslips, benefits, work schedules for Sainsburys.
Also, read:




Tuesday, July 23, 2019

JCP Associate Kiosk - www.jcpassociates.com - JCPENNEY

How to View Your Work Schedule on the JCPenney Associates Kiosk Website;

As a person who works for JCPenney (otherwise known as a JCPenney associate), it is possible for you to view your work schedules online. This is one of the things that you are able to do through the JCPenney Associates Kiosk website. It means that you can check online to know when you are supposed to be at work (and, conversely, when you can be off work). That, in turn, should make it easier for you to plan around your work schedules in a convenient manner.
How to get to the JCPenney Associates kiosk website:
To get to the JCPenney Associates kiosk website, you only need to enter that website’s address into your browser, and your browser will then transfer you to the said site (as long as your computer is connected to the Internet). If you check towards the end of this article, in the section entitled ‘links to get you going’, you will find the address we are making reference to that is, the address you can enter into your browser, in order to be taken to the JCPenney Employee Kiosk website.
How to log into the JCPenney Associates kiosk website:
When you get to the JCPenney Kiosk website, the first thing you will need to do is to specify that you are interested in the ‘Associate Kiosk @ Home’ function. So from the JCPenney Associates Home page, click on the first link: the ‘Associate Kiosk @ Home’ link. On the screen that you are taken to, log in to the JCPenney Employee Kiosk website by entering your employee ID and your password. If you don’t yet have a password for use on the site, click on the ‘Register as a New User’ link, and then go through the motions of registering (note that this will only work if you are working on a JCPenney workplace computer). Having thus registered, log in, by entering your employee ID and the password you will have created.
How to actually access your work schedule on the JCPenney Associates kiosk website:
After logging into the JCP Associates kiosk website, check on the left-hand side of the first page you are taken to, and you will see somewhere written ‘JTime Launchpad’. Click here, and you will be able to access and view your JCPenney work schedule online.
Sign Up for Electronic W-2 Forms On the JCP Associates Kiosk Website
As a JCPenney employee, it is possible for you to sign up for electronic W-2 forms, through the JCPenney Associates Kiosk website. By opting to receive W-2 forms electronically, you position yourself to benefit from the greater convenience associated with electronic filing of W-2 forms. As you may be aware, the W-2 form is essentially a tax form: one that is meant to be filled in by an employer, reporting an employee’s annual earnings and the amount of money that was deducted from those earnings as taxes. Now in your case, as a person who works for JCPenney (that is, as a JCP Kiosk associate), it is obviously JCPenney Kiosk that is supposed to fill in the W-2 form for you. And having done so, JCPenney is expected to provide you (as an employee) with one copy of the filled-in W-2 form, which you would then use for tax filing purposes. It is this aspect of receiving the W-2 form from JCPenney that can be undertaken electronically, through the JCPenney Associate Kiosk website.
Accessing the JCPenney Associates Kiosk website:
To access the JCPenney Associates Kiosk website, simply enter the address for that particular website into your browser, and your browser will transfer you to that site. The address we are making reference to, which you can enter into your browser in order to be transferred to the JCPenney Associates Kiosk website is provided/indicated towards the end of this article, in the section entitled ‘links to get you going’.
Signing up for electronic W-2 forms on the JCPenney Associates kiosk website:
When you get to the JCPenney Associates Kiosk website (specifically on the JCPenney Associates Home page), click on the third link on the page: that is the ‘EW-2 and reissues’ link. Then follow the simple instructions on the page that you are taken to, in order to successfully sign up for electronic W-2 forms.
What you need to do, after signing up for electronic W-2 forms on the JCPenney Associates kiosk website:
After signing up for electronic W-2 forms on the JCPenney Associates Kiosk website, you will only need to bid your time, as you wait for the W-2 form to become available. While signing up for electronic W-2 forms on the JCPenney Associates kiosk website, you will have encountered a ‘notification box’. The instructions in the notification box should guide you on the question of how to know when the W-2 form becomes available. So when the W-2 form becomes available, you will be able to access it through the JCPenney Associates Kiosk website, and you can subsequently use it for your tax filing purposes.

Enroll for Benefits Through the JCPenney Associates Kiosk Website:

As a JCPenney associate, you are eligible for certain benefits. These include medical benefits, dental benefits, term life insurance benefits and AD&D benefits (among others). The way some of these benefits schemes are run is such that you need to enroll, in order to start enjoying them. And as it turns out, enrolling for such benefits is one of the things you can do through the JCPenney Associates Kiosk website. That is specifically through a feature known as the PowerLine feature on the JCPenney Associates Kiosk website.
Getting to the JCPenney Associates kiosk website:
Getting to the JCPenney Associates kiosk website is simply a matter of entering the web address for the JCPenney Associates kiosk website into your browser’s address bar, and then hitting the ‘enter’ button on your keyboard. The web address in question, which you can enter into your browser’s address bar in order to access the JCPenney Associates Kiosk website is provided/indicated towards the end of this article, in the section entitled ‘links to get you going’.
Logging into the JCPenney Associates kiosk website:
Upon getting to the JCPenney Associates Kiosk website, you will land on the JCPenney Associates Home page.  Once you are there, click on the first link, that is, the ‘Associate Kiosk @ Home’ link. Then on the screen that you are transferred to upon clicking on that link, enter your employee ID and your password, and then click on the ‘Sign On’ button in order to be logged in.
Accessing the PowerLine feature on the JCPenney Associates kiosk website:
Having logged into the JCPenney Associates Kiosk, check towards the bottom right-hand side, and you will notice a ‘PowerLine’ link. Click on the said PowerLine link. You will then be taken to a page with more information on the various benefits you are eligible for.
Enrolling for benefits through JCPenney Associates Kiosk PowerLine:
On the page that you are taken to after clicking on the PowerLine link, click on the link for the specific benefit you are interested in. Upon doing so, you should be taken to a page with more information on that particular benefit, where you are also likely to find a link you can click on if you wish to actually enroll for that particular benefit.
Note that it also possible for you to access the PowerLine feature directly, by entering the web address for the JCPenney Powerline website (which takes you to the JCPenney benefits page on the Hewitt website). That address for direct access to the JCPenney Powerline website is also indicated towards the end of this article, in the section entitled ‘Links to get you going’.

Registering as a New User On the JCPenney Associate Kiosk Website:

Before you can start using the JCPenney Associate Kiosk website to access your employment information, you, first of all, need to register as a new user on the site. This is to say that you, as a person who works for JCPenney, can’t view your paystubs, work schedules, benefits information, W-2 information… or anything else through the JCPenney Associate Kiosk, before registering as a new user on the site. The JCPenney Associates Kiosk website is one where you have to be logged in, for you to view your personal information. And for you to be logged in, you need to have set up a password: which is something that you only get a chance to do in the process of registering as a new user on the JCPenney Associate Kiosk website.
Initiating the process of registering as a new user on the JCPenney Associate Kiosk website:
You initiate the process of registering as a new user on the JCPenney Associate Kiosk website by first loading the website and then navigating to the registration/sign up page.
To load the JCPenney Associate Kiosk website, enter the address for it into your browser. The address we are making reference to, which you can enter into your browser for the browser to load the JCPenney Associate Kiosk website is indicated/provided within the section entitled ‘links to get you going’ towards the end of this article.
When you enter the address for the JCPenney Associate Kiosk website into your browser, the browser will take you to the JCPenney Associates Homepage. From the said homepage, click on the first link — namely the ‘Associate Kiosk @ Home’ link. On the screen you are transferred to, to the right of the login box, you will see a couple of links: the ‘I forgot my password’ link and the ‘Register as a new user’ link. Click on the latter. But note that the link won’t go through if you are actually at home (that is if you are not on a JCPenney workplace computer).
Going through the process of registering as a new user on the JCPenney Associate Kiosk website:
After clicking on the ‘Register as a new user’ link, you only need to fill in the required bits of information, in order to complete the actual registration process. The hallmark of this process will be the selection/setting up of the password that you would henceforth be used to log into the JCPenney Associate Kiosk website.
Registering as a new user on the JCPenney Associate Kiosk website at home:
It is important to note that it is impossible for you to actually register as a new user on the JCPenney Associate Kiosk website if you are literally at home. JCPenney says that for security and privacy purposes, it deactivates the ‘I forgot my password’ and the ‘Register as a new user’ links for people who are trying to access the website from home computers. Thus, you can only be registering as a new user of the site through a JCPenney workplace computer.
The next steps after registering as a new user on the JCPenney Associate Kiosk website:
After registering as a new user on the JCPenney Associate Kiosk website, you will be logging into the site using the password that you set up in the course of the registration. So you would be entering your employee ID (as the username) and the password you set up in the course of the registration, whenever you wish to log in and access your information through the JCPenney Associate Kiosk website.
JCPenney Associate Discount Card Activation:
As a JCPenney associate, there are certain special discounts that are available to you (and your eligible dependents). Now for you to actually benefit from the said discounts when shopping in brick and mortar JCPenney stores, you need to have a special discount card. The card in question is known as the JCPenney Associate discount card.  When shopping online at JCP.com, the discounts are automatic. But when shopping in brick and mortar JCPenney stores, you have to show the discount card at the checkout point, for the discount to be applied. And for the JCPenney discount card to work, you need to have activated it. Now, as it turns out, the discount card activation is something you need to do on a JCPenney kiosk at work.
How to acquire the JCPenney Associate Discount card, in the first place
The JCPenney Associate Discount card is something you are given when you are first hired by JCPenney. In the course of what JCPenney refers to as the ‘new hire process’, one of the things you are given is a JCPenney Associate Discount card. Actually, you may end up getting several of these cards: one for yourself, and others for your eligible dependents. As for card replacements (and for additional cards, in case you get new eligible dependents in the course of your employment), you only need to contact your General Manager or your unit leader at work.
How to go about activating the JCPenney Associate Discount card
Having acquired the JCPenney Associate Discount card, the next logical thing for you to do is to activate it. The first step in that process is that of going to a JCPenney Kiosk (at work). The second step is to log into the JCPenney Kiosk. And having logged into the JCPenney Kiosk, you need to select the main menu, and from the main menu click on ‘self-service’ and then from the self-service sub-menu, click on ‘discount card activation’. You will then be able to activate the card, by entering a few details.
How to use the JCPenney Associate Discount card, after activation
Having activated the JCPenney Associate discount card, you need to carry it with you whenever you go shopping in a brick and mortar JCPenney outlet. Then, having picked the items you needed, you only need to show the card at the checkout point (you may also be asked for identification), and the applicable discount will then be applied. Take note that in the long run, the sums of money you stand to save through JCPenney Associate discounts can be appreciably huge. This means that the JCPenney Associates discounts program is one that is definitely worth taking advantage of.

Thursday, May 23, 2019

Java Constructor & Types of Constructor in Java with examples

1. What is a Constructor in Java?

Java constructor is just like a method in Java that is used to initialize the state of an object and will be invoked during the time of object creation.

Java constructor interview questions

2. What are the Rules for defining a constructor?

  1. Constructor name should be the same as the class name
  2. It cannot contain any return type
  3. It can have all Access Modifiers are allowed (private, public, protected, default)
  4. It Cannot have any Non Access Modifiers (final, static, abstract, synchronized)
  5. No return statement is allowed
  6. It can take any number of parameters
  7. A constructor can throw an exception, we can have a throws clause

3. What is the use of Private Constructors in Java?

When we use privately for a constructor then object for the class can only be created internally within the class, no outside class can create an object for this class. Using this we can restrict the caller from creating objects.
class PrivateConstructorExample
{
    /**
     * Private Constructor for preventing object creation
    from outside class
    **/
    private PrivateConstructorExample(){ }
    
    public void disp()
    {
        System.out.println("disp() method called");
    }
}
public class Sample 
{
    public static void main(String args[])
    {
        //Creating the object for the Private Constructor class
        PrivateConstructorExample pc = new PrivateConstructorExample();
        
        pc.disp();
    }
}
When we run the above code we will be getting the below exception.
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 The constructor PrivateConstructorExample() is not visible

 at Sample.main(Sample.java:19)

4. Can we have a Constructor in an Interface?

No, We cannot have a Constructor defined in an Interface.

Java constructor interview questions

5. What is Constructor Chaining in Java?

Constructor Chaining is nothing but calling one Constructor from another. this keyword is used to call the current class constructor and the super keyword is used to call the parent class constructor.
class Parent
{
    public Parent()
    {
        System.out.println("Parent class no-args constructor called");
    }
    public Parent(String name)
    {
        System.out.println("Parent class Parameterized constructor called by "+name);
    }
}
public class Child extends Parent
{
    public Child()
    {
        this("JIP");
        System.out.println("Child class no-args constructor called");
    }
    public Child(String name)
    {
        super("JIP");
        System.out.println("Child class Parameterized constructor called by "+name);
    }
    public static void main(String args[])
    {
        Child c = new Child();
    }
}
Output :
Parent class Parameterized constructor called by JIP
Child class Parameterized constructor called by JIP
Child class no-args constructor called

6. Can we have this and super in the same constructor?

No, we cannot have this and super in the same constructor as anyone only can be in the first line of the constructor.
class Parent
{
    public Parent()
    {
        System.out.println("Parent class no-args constructor");
    }
}
public class Child extends Parent
{
    public Child()
    {
        this("JIP");
        super();
        System.out.println("Child class no-args constructor");
    }
    public Child(String name)
    {
        
        System.out.println("Child class Parameterized constructor"+name);
    }
    public static void main(String args[])
    {
        Child c = new Child();
    }
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 Constructor call must be the first statement in a constructor

 at Child.(Child.java:13)
 at Child.main(Child.java:23)

Java constructor interview questions

7. Is it possible to call a subclass constructor from superclass constructor?

No. You cannot call a subclass constructor from a superclass constructor.

8. What is a No-arg constructor?

Constructor without arguments is called the no-arg constructor. In Java Default constructor is a no-arg constructor.
class Demo
{
    public Demo()
    {
        //No-arg constructor
    }
}

9. Can we have a class with no Constructor in it? What will happen during object creation?

Yes, we can have a class with no constructor, When the compiler encounters a class with no constructor then it will automatically create a default constructor for you.

10. Can we have both Default Constructor and Parameterized Constructor in the same class?

Yes, we have both Default Constructor and Parameterized Constructor in the same class.

Java constructor interview questions

11. Can a Constructor return any value?

A Constructor cannot return any explicit value but implicitly it will be returning the instance of the class.

12. Will compiler create the Default Constructor when we already have a Constructor defined in the class?

No, the compiler will not create the Default Constructor when we already have a Constructor defined.

13. Can an abstract class in Java have a constructor?

Yes, an abstract class can have a constructor. The below code works perfectly fine.
abstract class Demo1 { 
    String value;
    public Demo1( String value ) {
        this.value = value;
    }
    public String getValue()
    {
     return value;
    }
}
public class Test extends Demo1 {
    public Test() {
        super("CoreJava");
    }
}

Java constructor interview questions

14. What happens when a Constructor is defined as “protected”?

In general protected method can be accessed by other class in a different package only through Inheritance. But when you assign protected access to a constructor it behaves a bit different. It can be accessed only by a call of super() (according to JLS) and not directly by any other means.
package com.javainterviewpoint;

public class Parent
{
    protected Parent()
    {
        System.out.println("Parent Constructor called");
    }
    public void parentDisp()
    {
        System.out.println("Parent Disp called");
    }
}
package com.javainterviewpoint1;

import com.javainterviewpoint.Parent;

public class Child extends Parent
{
    public Child()
    {
        /**
         * Using super() Parent Class protected constructor can be called
         */
        super(); 
        System.out.println("Child Constructor called");
    }
    public void childDisp()
    {
        System.out.println("Child Disp called");
    }
    public static void main(String args[])
    {
        /**
         * Even though we have extended Parent class in Child class, 
         * below way of calling Parent class Constructor is not allowed
         * 
         * The constructor Parent() is not visible - error will be thrown
         */
        Parent p = new Parent() // Error will be thrown
    }
}

15. Why constructors cannot be final in Java?

When you set a method as final, then” The method cannot be overridden by any class”, but Constructor by JLS ( Java Language Specification ) definition can’t be overridden. A constructor is not inherited, so there is no need for declaring it as final.

16. Why constructors cannot be abstract in Java?

When you set a method as abstract, then “The method doesn’t or cannot have the body”. A constructor will be automatically called when the object is created. It cannot lack a body moreover an abstract constructor could never be implemented.

17. Why constructors cannot be static in Java?

When you set a method as static, it means “The Method belongs to a class and not to any particular object” but a constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static.
Also Read: Java Interview Questions class and object in Java Java Constructor Wells Fargo Routing Number Bank of America Routing Number

Java String Interview Questions and Answers

1) What is String in Java? Is String is data type? String in Java is not a primitive data type like int, long or double. The string is a ...