If you’re anything like me, you’ve walked into an interview feeling like you’re prepared. You’ve spoken confidently, you’ve made eye contact and everything seems to be going well, before you’re hit with a bombshell; a technical question. Suddenly your mind goes blank– objects? What’s an object again? You might lose all credibility in this case. This blog post is mainly a place where I’ll be updating periodically with new questions related to Java that I want to review before an interview, but you might want to review it too. Or not, it’s totally up to you; but if it helps either of us get a job, it’ll all be worth it.
Note: To see the answers, please copy the tiny text in the blue box following that specific question and paste it into your text editor of choice. Bonus: Git-related questions have their answers colored purple!
- Name a reason you could choose to use nested/inner classes.
Essentially nested/inner classes are used to make your code more maintainable and readable.
- Why would you make a class abstract?
Abstract class can provide some incomplete functionality or code that will be flushed out in subclasses. You can create a new subclass without editing the rest.
- What is an interface?
Interfaces are another way to achieve abstraction, being a completely abstract class that groups related methods with empty bodies; interface that implements an interface has it’s own definition for each inherited method
- What is a difference between abstract class and interface?
Although they are fundamentally similar, there are several differences between abstract classes and interfaces, for example:
-Abstract classes can make use of access modifiers; objects and methods declared in an interface are public by default
-Variables declared in an interface are final by default, while this is not the case for abstract classes
-Interfaces are completely concrete which means that they cannot have concrete methods which contain code; meanwhile abstract classes can have both abstract methods and concrete methods
-Abstract classes can extend(inherit from) only one class while interfaces can implement as many other interfaces as needed
- Can you do multiple inheritance in Java? How?
Yes, by utilizing interfaces
- How do you instantiate a scanner object to expect input from a user?
Scanner [Scanner Name] = new Scanner(system.in);
- What kinds of streams can a scanner object accept?
Any, including files!
- Can you subtract dates from each other using the Date package?
Not implicitly, but there are a few methods in the Date package that can be used to achieve this.
- Why are Java wrapper classes immutable?
They create a new object instead of updating the old.
- What does the File class represent in your PC?
The File class can be considered to be an abstract representation of your PC’s file explorer. You can navigate and manipulate files using the File class, just as you can do in File Explorer for example.
- What are the contents of a Git config file?
git config is the command you use in Git to set your name and e-mail address to be noted each time you commit to a project; the contents of a git config is this name and e-mail address.
- Does creating a repository with git init give you an empty folder?
No, repositories created by entering git init give you a .git directory with sub-directories that include objects, refs/heads, refs/tags and template files.
- What does git rm do?
git rm can be used to remove files/repositories from your working directory and stages the deletion for your commit/push.
- Why would we use git log?
git log lists the version history for the current branch when it is called. This is extremely useful, for example, if you’re attempting to find a previous version when a certain feature was added in your code that messes with another part of your code. Knowing which version it was automatically by calling git lot without needing to dig yourself to get a list of every version is greatly efficient.
- How do you check hierarchy of a tag?
git show [tag name here]^{tree}
- Why would you use tags?
Say it with me; versioning! Tags can help you quickly locate different commits and versions.
- If we have three items in git stash, and we don’t specify index number which one will it affect?
The most recently added item; git stash is a FILO system(First-in-last-out).
- What is a static variable?
A static variable is a variable which belongs to the class; this means that any object of the class can access it.
- Is there a command to list the branches that are merged in Git?
git branch –merge
git branch (without the –merge) will also list all branches, merged and non-merged!
- Importance of git branch/git checkout?
It is best practice to avoid updating the master branch while you are collaborating on a project with several teammates using git. These commands help you by making sure you only update a certain branch(copy) of the project and later you can merge your changes with the master. It helps to avoid version conflicts and general confusion as well!
- Difference between class method and instance method.
Class method is a method defined within a class, it belongs to the class—instance method is used inside of an instantiated object, it belongs to that object.
- Why would you want to use inheritance?
The main selling point of inheritance is that it promotes re-usability!
- What is a proper use for comments?
A comment can be mainly used to give a brief description of what your code is doing; this is useful not only for your future reference( “what was I coding again?” ) but for anyone who reads your code.
- Is an object a reference?
Yes, it points to a location in memory where the data is actually stored.
- True or False: Implicit type casting will most likely make you lose data.
False; implicit is when you go from smaller data types to larger; the larger data type can handle the data easily from the smaller. Reversely, explicit type casing, going from larger data types to smaller is where you can lose data because a byte can handle only a fraction of the range of an int, etc.
- What does system.in do?
Tells the program to expect user input from the keyboard
- Why do you use ‘new’?
New tells the program that you’re declaring a new object in your class.
- What is the purpose of a constructor?
The purpose of a constructor is to initialize an object in a class, noticeably different from methods which perform some task when called.
- What’s the difference between = and ==?
A single = is an assignment operator, used to give a value to a variable. == is used to check if a value is equal to another value.
- What happens if there are no break statements in a switch?
If there are no break statements in a switch, the switch will output the last case regardless of argument.
- What if your while loop can’t reach the limit you set in your counter?
If your limit loop is unreachable (if it skips over for example), the loop will just continue iterating until you manually stop it or your computer crashes from reaching the limit. It’s extremely important to set a reachable end in your while loop!
- Are you able to skip elements in an array?
No, you must iterate through every element in an array.
- What is the purpose of the finalize() method? Why should we avoid using it?
finalize() is a method of java.lang.Object. It can be overridden to close object operations in memory before the object is removed and clean up resources. While it might sound like a nifty time-saving method, it’s unreliable based on when it runs or if it even runs. If you rely on it to free up resources in your code, you might run out of those resources while compiling.
- What is a singleton?
A singleton is a design pattern where a class has only one object, which is an instance of the class itself.
- Describe what the JRE is.
- Describe what the JDK is.
- Describe what the JVM is.