Java Programming test questions and answers
Top 30 Programming interview questions Show
I have not given answers to these programming questions but those can be found by Google and I will try to post links of answers here sometime later but at the same time, I will try to provide quick tips or hints on some questions. Anyone who is following programming questions must be familiar with these questions and also knows the answer for most of these but for new guys and even for intermediate it's worth refreshing it before going to any programming job interview like Core Java interview. Also, basic knowledge of essential data structure and algorithms is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course like Data Structures and Algorithms: Deep Dive Using Java on Udemy to improve your knowledge and algorithms skills. 1. String Programming Interview QuestionsThe string is a primary and probably most common thing you come across on any programming language and so is with any programming interview. There is almost always a question on String whether its related to the length or replace but I have always found one or two String programming questions on interviews. 1) Write code to check a String is palindrome or not?
(solution) 2) Write a method which will remove any given character from a String?
(solution) 3) Print all permutation of String both iterative and Recursive way? (solution) 4) Write a function to find out longest palindrome in a given string? (solution) 5) How to find the first non repeated character of a given String? (solution) 6) How to count the occurrence of a given character in a String? (solution) 7) How to check if two String are Anagram? (solution) 8) How to convert numeric String to int in Java? (solution) Some more String related Questions which mostly appear in Java programming interviews: 1) What is the difference between String, StringBuilder, and StringBuffer in Java? (answer) 2) Why String is final in Java? (answer) 3) How to Split String in Java? (answer) 4) Why Char array is preferred over String for storing password? (answer) These questions help improve your knowledge of string as a data structure. If you can solve all these String questions without any help then you are in good shape. If you want to learn more about String and other data structure then I suggest you check out the Master the Coding Interview: Data Structures + Algorithms course by Andrei Negaoieon ZTM Academy. It's a great course to revise fundamentals before interview. If you need more practice, here is another list of 20 string coding questions. 2. Programming questions on ArrayAn array is one of the topics where most of the programming questions are asked. There are many and many programming questions on Array and here I have included only some of them which are not very difficult to solve but some of the array programming questions can be extremely challenging, so well prepare this topic. 9) In an array 1-100 numbers are stored, one number is missing how do you find it? (solution) 10) In an array 1-100 exactly one number is duplicate how do you find it? (solution)
11) In an array 1-100 multiple numbers are duplicates, how do you find it? (solution) 12) Given two arrays, 1,2,3,4,5 and 2,3,1,0,5 find which number is not present in the second
array. 13) How do you find the second highest number in an integer array? (solution) 14) How to find all pairs in an array of integers whose sum is equal to the given number? (solution) 15) How to remove duplicate elements from the array in Java? (solution) 16) How to find the largest and smallest number in an array? (solution) 17) How to find the top two maximum number in an array? (solution) These questions will not only help you to develop your problem-solving skills but also improve your knowledge of array data structure. If you need more advanced questions based upon array then you can see also see The Coding Interview Bootcamp: Algorithms + Data Structures, a bootcamp style course on algorithms, especially designed for interview preparation to get a job on technical giants like Google, Microsoft, Apple, Facebook etc. And, if you feel 10 is not enough questions and you need more practice, then you can also check out this list of 30 array questions. 3. LinkedList Programming Interview QuestionsA linked list is another important data structure after array and String. It actually compliments array and whatever you cannot do with an array, you can do with a linked list. For example, the array needs contiguous memory to store objects but the linked list doesn't need that. It's difficult to add and remove elements in an array because you need to shift existing elements but that is very easy with a linked list, as you just need to change the pointer to accommodate them. But, nothing is free in this world. While linked list provides all these functionalities but the cost of that you lose the ability to search elements in constant time with index. Searching and element require traversing linked list, which means examining all nodes, thus cost around O(n) time. After this answer interviewer will ask you to find the middle element in single pass and there you can explain that by doing space-time trade-off you can use two pointers one incrementing one step at a time and other incrementing two-step a time, so when the first pointer reaches end of linked second pointer will point to the middle element. 15) How do you find the 3rd element from last in a single pass? (solution) 16) How do you find if there is any
loop in a singly linked list? How do you find the start of the loop? (solution) 17) How do you reverse a singly linked list? (solution) 18) Difference between a linked list and array data structure? (answer) If you are having trouble solving these linked list coding questions then I suggest you refresh your data structure and algorithms skill by going through Grokking the Coding Interview: Patterns for Coding Questions course on Educative, one of the best course to learn coding patterns like Sliding Window and Merge intervals, which can be used to solve 100+ Leetcode problems. If you need more linked list based questions then you can also check out this list of 30 linked list interview questions for more practice questions. 4. Binary Tree Programming Interview QuestionsBinary tree or simply tree is one of favorite topic for most of the interviewer and pose a real challenge if you struggle with recursion. Programming questions on the tree can become increasingly difficult when you think iterative but sometimes can be very easy if you come with a recursive solution. 18) How do you find the depth of a binary tree? (solution) 19) Write code to print InOrder traversal of a tree? (solution) 20) Print out all leaf node of a binary tree? (solution) 21) Write a method in Java to check if a tree is a binary search tree or not? (solution) 22) How to check if a tree is balanced or not in Java? (solution) 24) How do you perform preorder traversal in a given binary tree? (solution) 25) How do you traverse a given binary tree in preorder without recursion? (solution) 26) How do you print all nodes of a given binary tree using inorder traversal without recursion? (solution) 27) How do you implement a postorder traversal algorithm? (solution) 28) How do you traverse a binary tree in postorder traversal without recursion? (solution) 29) How are all leaves of a binary search tree printed? (solution) 40) How do you count a number of leaf nodes in a given binary tree? (solution) 41) How do you perform a binary search in a given array? (solution) Binary tree based questions sometimes get trick and if you are having trouble solving these tree-based list coding questions then I suggest you revise your data structure and algorithms skill by going through Software Engineering Interview Course on Exponent. This course provides a comprehensive review of the most important data structures, algorithms, and system design principles and also provides mock interviews with FAANG engineers and managers showcasing what interviews look like, as well as interactive coding problems in Python and JavaScript. Plus, there are extra lessons on behavioral interview questions for engineers, and advice on growing your tech career. It's written by an ex-FAANG engineers and it is one of the most comprehensive course to revise all important data structures like an array, linked list, binary tree etc. 5. Programming Questions on Searching and SortingI have only included two programming questions related to searching and sorting but there are more can be found on Google. Purpose of these programming questions is to see whether a programmer is familiar with the essential search and sort mechanism or not. 23) Write a program to sort numbers in place using quick sort? (solution) 24) Write a program to implement a binary search algorithm in Java or C++? (solution) 25) How do you sort Java objects using a Comparator? (answer) 26) Write code to implement Insertion Sort in Java? (solution) 27) Write code to implement Bubble Sort in Java? (solution) If you can solve these questions easily then you are in good shape. For more advanced questions, I suggest you solve problems given in the Algorithm Design Manual by Steven Skiena, a book with the toughest algorithm questions. 6. Programming Questions on NumbersMost of the programming questions are based on numbers and these are the ones which most of us did on the college level and mind you they still have value I have seen programmers with experience of 3 years struggle with these programming questions and doesn't solve it some time and take a lot of time which simply shows that they are not in programming in there day to day work. 26) Write code to check whether a no is a power of two or not? (solution) 27) Write a program to check whether a number is a palindrome or not? (solution) 28)
Write code to check whether an integer is Armstrong number or not? (solution) 29) Write a program to find all prime number up to a given number?
(solution) 30) Write a function to compute Nth Fibonacci number? Both iterative and recursive?
(solution) 31) How to check if a number is binary? (solution) 32) How to reverse an integer in Java? (solution) 33) How to count a number of set bits in given integer? (solution) 34) How to find the sum of digits of a number using recursion? (solution) 35) How to swap two numbers without using temp variable? (solution) 36) How to find the largest of three integers in Java? (solution) 37) Write a program to find prime factors of an integer? (solution) 38) How to add two integers without using arithmetic operator? (solution) If you need more such coding questions you can take help from books like Cracking the Coding Interview book by Gayle Lakman McDowell which presents 189+ Programming questions and solution. A good book to prepare for programming job interviews in a short time. 7. General Programming Interview QuestionsIn this category of programming questions, I have put questions which are not fit into any data structure but present a real-life problem and you need to provide a solution. These programming questions are sometimes based on problems faced by the developer itself. I have not included many Software design-related programming question which I have shared on Top 20 software design questions and answers; you can also check that. 31) Write a program to find out if two rectangles R1 and R2 are overlapping? (solution) 32) You need to write a function to climb n steps you can climb
either 1 step at a time or 2 steps a time, write a function to return a number of ways to climb a ladder with n step. (solution) 33) Write code for Generate Random No in a range from min to max? (solution) 34) Write a program for word-wrap which should work on any screen size? (solution) 35) Design an algorithm to find the frequency of occurrence of a word in an article? (solution) 36) Write a program to implement a blocking queue in Java? (solution) 37) Write a program for the producer-consumer problem? (solution) 8. Books to prepare for Programming Job InterviewsThere are a lot of good books available, which can help the programmer to do well on Interviews. Here is a list of books, which I personally prefer, in order, I like them. 1. Programming Interviews Exposed: Secrets to Landing Your Next JobA must-read books for both beginners and experienced programmers alike. It not only help you to do well on interviews but also on negotiation, answering general questions etc. This book contains a collection of questions from a wide range of programming topics, including data structure, algorithms, strings, Java, networking, database, SQL, object-oriented programming, software design etc. This book will give you the whole picture of what can be asked. 3. Top 10 coding interview problems asked in Google with solutions: Algorithmic ApproachThis is the must read a book, if you are preparing for Google interview, or something along the line e.g. Facebook, Amazon or Microsoft Interviews. It contains top 10 programming problems, frequently asked at Google with detailed worked out a solution, explanation in both pseudocodes and in C++. 9. Tips on answering Programming questionsInterviews are not ready and even if you know the answers you need to keep some things in mind while answering the questions or solving problems. The interviewer often likes to see your ability to solve unknown problems and how you react when a new challenge presented. For example, if you wrote a recursive solution then they will ask you to solve without recursion, if you use additional memory then you will ask you to solve without that and in-place, mostly in case of an array and linked list problems. Here are some of the tips to do well on your programming interview: 1. If Interviewer asks you to write function then make sure you do some necessary check for bad input e.g. null check or empty check. Most of the time programmer forgets to test for not null, empty, less than 1, greater than 1 or zero input. 2. If you write an iterative version of function then Interviewer may ask you to write recursive version or vice-versa so be prepared for that. 3. If you write a recursive function then Interviewer will ask to optimize it, even in case of Iterative version. So remember that you can optimize recursive function by Memorization (caching already calculated value) and by applying some space/time tradeoff principle. For example, recursive version of Fibonacci series has O(n ^2) time performance which can be reduced to O(n) using Memoziation. 4. The interviewer may ask you to calculate Order of complexity for best and worst case of any method so be prepared. 5. Most of the time Interviewer ask how to fix a problem as follow-up question e.g. he will ask how do you find deadlock and then how to fix deadlock in java etc. These are just some of the tips you can follow to be successful in you programming Job interviews. As I told, it's not enough just answering the questions, you need to be attentive and see how the interviewer is reacting. Is he getting pleased with your approach you are not irritating him off by asking silly questions, you need to be attentive to your surrounding? If you are a fresher or a junior developer and haven't had much programming job interview experience, I suggest you go through Grokking Modern System Design for Software Engineers & Manager course on Educative. One of the best course to master one of the toughest topic on Programming interview, System Design. Apart from these programming interview questions you can also checkout these 25 Software Design questions and some of the other Java programming questions I have already discussed in my blog. You can also take help from Programming Interviews Exposed and Cracking the Coding Interview book with a solution to prepare for any programming Job interview. Those two books have helped me a lot in the past and even today I read them whenever I need to refresh my concepts. 10. Now You’re Ready for the Programming InterviewThese are some of the most common questions outside of data structure and algorithms that help you to do really well in your interview. I have also shared a lot of these questions on my blog, so if you are really interested, you can always search for them. These common coding, data structure, and algorithm questions are the ones you need to know to successfully interview with any company, big or small, for any level of programming job. If you are looking for a programming or software development job, you can start your preparation with this list of coding questions. This list provides good topics to prepare and also helps assess your preparation to find out your areas of strength and weakness. As I said before, a good knowledge of data structure and algorithms is important for success in coding interviews and that’s where you should focus most of your attention. Closing Notes If you like this article, then please share with your friends and colleagues, and don’t forget to follow javinpaul on Twitter! What is Java basic questions and answers?Most Popular Java Interview Questions and Answers. Q #1) What is JAVA? ... . Q #2) What are the features of JAVA? ... . Q #3) How does Java enable high performance? ... . Q #4) Name the Java IDE's? ... . Q #5) What do you mean by Constructor? ... . Q #6) What is meant by the Local variable and the Instance variable? ... . Q #7) What is a Class?. How do I study for a Java test?So, without wasting any more time, here are my five tips to prepare well for the OCAJP and OCPJP exams.. Code Every Day. There is no substitute for coding. ... . Join a Good Course. ... . Read a Good Study Book. ... . Choose a Good Exam Simulator. ... . Regularly Participate on Forums. ... . Solve As Many Practice Questions As Possible.. What is Java assessment Test?This Java test is a technical pre-employment test to analyze a candidate's knowledge and proficiency in Java. These assessments determine how well-versed applicants are in programming Java algorithms by assessing their basic programming skills and domain knowledge.
What questions are asked in coding test?This section covers some coding interview questions that test the conceptual understanding of the candidate.. What is a Data Structure? ... . What is an Array? ... . What is a Linked List? ... . What is LIFO? ... . What is a Stack? ... . What is FIFO? ... . What is a Queue? ... . What are Binary Trees?. |