java数据结构测试题及答案解析

合集下载

数据结构JAVA语言描述习题答案(刘小晶等主编).pdf总复习

数据结构JAVA语言描述习题答案(刘小晶等主编).pdf总复习
设定哈希函数 H(key) = key MOD 11 ( 表长=11 ) 若采用二次探测再散列处理冲突
0 1 2 3 4 5 6 7 8 9 10
55 01 23 14 36 82 68
19
11
3
1 1 2 1 2 1 4 1 ASL(成功)= (1*5+2*1+3+4)/9=14/9
3 V4 4 V5 5 V6
1 5 5 4
4
广度优先搜索法遍历序列: V1,V2,V3,V4,V5,V6
2
3
例题: 设有如下的两个网络, 分别用普里姆(Prim)算法 和克鲁斯卡尔(Kruskal)算法具体构造相应的最小生 成树。 写出过程。 a 5 6 1
b
5 6
c
6
5 4
d
3
2
[例1]假设按低下标优先存储整数数组 A9×3×5×8时, 第一个元素的字节地址是100,每个整数占 四个字节,问元素a3125的地址是什么? LOC(a3125)= ? 100+(3×3×5×8+1×5×8+2×8+5)×4 =1784
[ 例 2] 设有数组 A[1..8,1..10] ,数组的每个元素占 3 字节,数组从内存首地址 BA开始以列序为主序顺 序存放,求数组元素 a[5,8]的存储首地址.
8 9 10
55 01 23 14 68 11 82 36 19
1 1 2 1 3 6 2 5 1 查找次数 ASL(成功)=(4*1+2*2+3+5+6)/9=22/9
ASL(不成功)=(10+9+…+1+1)/11=56/11
例如: 关键字集合 { 19, 01, 23, 14, 55, 68, 11, 82, 36 }

Java测试题(含答案和部分解析)

Java测试题(含答案和部分解析)

Java测试题(含答案和部分解析)一、选择题1. 以下哪个不是Java基本数据类型?A. intB. charC. StringD. boolean答案:C解析:Java的基本数据类型包括:int, byte, short, long, float, double, char, boolean。

String是一个类,不是基本数据类型。

2. 在Java中,下列哪个操作符用于取模?A. %B. /C.D. -答案:A解析:取模操作符是%,用于获取两个数相除的余数。

3. 以下哪个方法可以实现字符串的截取?A. substring(int start, int end)B. substring(int start)C. substring(int start, int length)D. substring(int index)答案:A解析:substring(int start, int end)方法用于截取字符串,从start开始到end结束(不包括end)。

4. 以下哪个方法用于判断字符串是否为空?A. isEmpty()B. isBlank()C. isEmptyD. isNull()答案:A解析:isEmpty()方法是String类的一部分,用于判断字符串是否为空。

二、填空题5. 在Java中,一个类中的构造方法如果没有显式定义,系统会自动提供一个默认的构造方法,该构造方法称为______。

答案:默认构造方法6. 在Java中,如果一个方法没有返回值,则其返回类型应定义为______。

答案:void7. 在Java中,用于表示正则表达式的类是______。

答案:Pattern三、判断题8. 在Java中,所有的类都默认继承自Object类。

答案:正确9. Java中的静态变量在类加载时初始化。

答案:正确10. 在Java中,可以使用一个对象的引用调用另一个对象的成员变量。

答案:错误解析:对象的引用只能调用该对象自己的成员变量或方法。

数据结构(Java版)_郑州大学中国大学mooc课后章节答案期末考试题库2023年

数据结构(Java版)_郑州大学中国大学mooc课后章节答案期末考试题库2023年

数据结构(Java版)_郑州大学中国大学mooc课后章节答案期末考试题库2023年1.对不含相同元素的同一输入序列进行两组不同的、合法的入栈和出栈组合操作,所得的输出序列一定相同。

参考答案:错误2.在链队列中,即使不设置尾指针,也能进行入队操作。

参考答案:正确3.循环顺序队列和循环链队列都存在空间一处问题。

参考答案:错误4.直接选择排序的时间复杂度与关键字的初始排列无关。

参考答案:正确5.一个循环链表可以由给定的头指针或尾指针来唯一标识。

参考答案:正确6.所谓随机存取,就是通过首地址和元素的序号可以在O(1)的时间内找到指定的元素。

参考答案:正确7.快速排序在最坏情况下的时间复杂度是O(【图片】)。

参考答案:正确8.哈夫曼树是带权路径长度最短的树,路径上权值较大的结点离根较近()参考答案:正确9.在队列中存取数据元素的原则是()。

参考答案:先进先出10.将整数1、2、3、4依次进栈,则不可能得到的出栈序列是()。

参考答案:142311.完全二叉树的存储结构通常采用顺序存储结构()。

参考答案:正确12.在中序线索二叉树中,每一非空的线索均指向其祖先结点()参考答案:正确13.二叉树中序线索化后,不存在空指针域()参考答案:错误14.二叉树的层次遍历需要栈结构的支持。

参考答案:错误15.下列关于AOE网的叙述中,不正确的是()参考答案:任何一个关键活动提前完成,那么整个工程将会提前完成16.一棵非空的二叉树的先序遍历序列与后序遍历序列正好相反,则该二叉树一定满足()参考答案:只有一个叶子结点17.引入二叉线索树的目的是()参考答案:加快查找结点的前驱或后继的速度18.单源最短路径算法的时间复杂度为()参考答案:O()19.对6个不同的数据元素进行直接插入排序,最多需要进行()次关键字的比较。

参考答案:1520.完全二叉树中,若一个结点没有左孩子,则它必是树叶()。

参考答案:正确21.已知循环队列存储在一维数组A[0【图片】n]中,且队列非空时front和rear分别指向队首元素和队尾元素。

JAVA数据结构习题及解答(英)

JAVA数据结构习题及解答(英)

QuestionsThese questions are intended as a self-test for readers.Answers to the questions may be found in Appendix C.1.In many data structures you can________a single record,_________it,and_______it.2.Rearranging the contents of a data structure into a certain order is called_________.30CHAPTER1Overview3.In a database,a field isa.a specific data item.b.a specific object.c.part of a record.d.part of an algorithm.4.The field used when searching for a particular record is the______________.5.In object-oriented programming,an objecta.is a class.b.may contain data and methods.c.is a program.d.may contain classes.6.A classa.is a blueprint for many objects.b.represents a specific real-world object.c.will hold specific values in its fields.d.specifies the type of a method.7.In Java,a class specificationa.creates objects.b.requires the keyword new.c.creates references.d.none of the above.8.When an object wants to do something,it uses a________.9.In Java,accessing an object’s methods requires the_____operator.10.In Java,boolean and byte are_____________.(There are no experiments or programming projects for Chapter1.)Questions31Chapter1,OverviewAnswers to Questions1.insert,search for,delete2.sorting3.c4.search key5.b6.a7.d8.method9.dot10.data typesQuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.Inserting an item into an unordered arraya.takes time proportional to the size of the array.b.requires multiple comparisons.c.requires shifting other items to make room.d.takes the same time no matter how many items there are.2.True or False:When you delete an item from an unordered array,in most cases you shift other items to fill in the gap.3.In an unordered array,allowing duplicatesa.increases times for all operations.b.increases search times in some situations.c.always increases insertion times.d.sometimes decreases insertion times.4.True or False:In an unordered array,it’s generally faster to find out an item is not in the array than to find out it is.5.Creating an array in Java requires using the keyword________.6.If class A is going to use class B for something,thena.class A’s methods should be easy to understand.b.it’s preferable if class B communicates with the program’s user.c.the more complex operations should be placed in class A.d.the more work that class B can do,the better.7.When class A is using class B for something,the methods and fields class A can access in class B are called class B’s__________.74CHAPTER2Arrays8.Ordered arrays,compared with unordered arrays,area.much quicker at deletion.b.quicker at insertion.c.quicker to create.d.quicker at searching.9.A logarithm is the inverse of_____________.10.The base10logarithm of1,000is_____.11.The maximum number of elements that must be examined to complete a binary search in an array of200elements isa.200.b.8.c.1.d.13.12.The base2logarithm of64is______.13.True or False:The base2logarithm of100is2.14.Big O notation tellsa.how the speed of an algorithm relates to the number of items.b.the running time of an algorithm for a given size data structure.c.the running time of an algorithm for a given number of items.d.how the size of a data structure relates to the number of items.15.O(1)means a process operates in_________time.16.Either variables of primitive types or_________can be placed in an array. Chapter2,ArraysAnswers to Questions1.d2.True3.b4.False5.new6.d740APPENDIX C Answers to Questions7.interface8.d9.raising to a power10.311.812.613.False14.a15.constant16.objectsuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.puter sorting algorithms are more limited than humans in thata.humans are better at inventing new algorithms.puters can handle only a fixed amount of data.c.humans know what to sort,whereas computers need to be told.puters can compare only two things at a time.2.The two basic operations in simple sorting are_________items and_________ them(or sometimes_________them).3.True or False:The bubble sort always ends up comparing every item with every other item.4.The bubble sort algorithm alternates betweenparing and swapping.b.moving and copying.c.moving and comparing.d.copying and comparing.5.True or False:If there are N items,the bubble sort makes exactly N*N comparisons.Questions1096.In the selection sort,a.the largest keys accumulate on the left(low indices).b.a minimum key is repeatedly discovered.c.a number of items must be shifted to insert each item in its correctlysorted position.d.the sorted items accumulate on the right.7.True or False:If,in a particular sorting situation,swaps take much longer than comparisons,the selection sort is about twice as fast as the bubble sort.8.A copy is________times as fast as a swap.9.What is the invariant in the selection sort?10.In the insertion sort,the“marked player”described in the text corresponds to which variable in the insertSort.java program?a.inb.outc.tempd.a[out]11.In the insertion sort,“partially sorted”means thata.some items are already sorted,but they may need to be moved.b.most items are in their final sorted positions,but a few still need to be sorted.c.only some of the items are sorted.d.group items are sorted among themselves,but items outside the groupmay need to be inserted in it.12.Shifting a group of items left or right requires repeated__________.13.In the insertion sort,after an item is inserted in the partially sorted group,it willa.never be moved again.b.never be shifted to the left.c.often be moved out of this group.d.find that its group is steadily shrinking.110CHAPTER3Simple Sorting14.The invariant in the insertion sort is that________.15.Stability might refer toa.items with secondary keys being excluded from a sort.b.keeping cities sorted by increasing population within each state,in a sortby state.c.keeping the same first names matched with the same last names.d.items keeping the same order of primary keys without regard to secondary keys.Chapter3,Simple SortingAnswers to Questions1.dparing and swapping(or copying)3.False4.a5.False6.b7.False8.three9.Items with indices less than or equal to outer are sorted.10.c11.d12.copies13.b14.Items with indices less than outer are partially sorted.15.buestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.Suppose you push10,20,30,and40onto the stack.Then you pop three items. Which one is left on the stack?2.Which of the following is true?a.The pop operation on a stack is considerably simpler than the remove operation on a queue.b.The contents of a queue can wrap around,while those of a stack cannot.c.The top of a stack corresponds to the front of a queue.d.In both the stack and the queue,items removed in sequence are takenfrom increasingly high index cells in the array.3.What do LIFO and FIFO mean?4.True or False:A stack or a queue often serves as the underlying mechanism on which an ADT array is based.5.Assume an array is numbered with index0on the left.A queue representing a line of movie-goers,with the first to arrive numbered1,has the ticket window on the right.Thena.there is no numerical correspondence between the index numbers andthe movie-goer numbers.b.the array index numbers and the movie-goer numbers increase inopposite left-right directions.c.the array index numbers correspond numerically to the locations in theline of movie-goers.d.the movie-goers and the items in the array move in the same direction.6.As other items are inserted and removed,does a particular item in a queue move along the array from lower to higher indices,or higher to lower?7.Suppose you insert15,25,35,and45into a queue.Then you remove three items.Which one is left?8.True or False:Pushing and popping items on a stack and inserting and removing items in a queue all take O(N)time.174CHAPTER4Stacks and Queues9.A queue might be used to holda.the items to be sorted in an insertion sort.b.reports of a variety of imminent attacks on the star ship Enterprise.c.keystrokes made by a computer user writing a letter.d.symbols in an algebraic expression being evaluated.10.Inserting an item into a typical priority queue takes what big O time?11.The term priority in a priority queue means thata.the highest priority items are inserted first.b.the programmer must prioritize access to the underlying array.c.the underlying array is sorted by the priority of the items.d.the lowest priority items are deleted first.12.True or False:At least one of the methods in the priorityQ.java program (Listing4.6)uses a linear search.13.One difference between a priority queue and an ordered array is thata.the lowest-priority item cannot be extracted easily from the array as it can from the priority queue.b.the array must be ordered while the priority queue need not be.c.the highest priority item can be extracted easily from the priority queue but not from the array.d.All of the above.14.Suppose you based a priority queue class on the OrdArray class in the orderedArray.java program(Listing2.4)in Chapter2,“Arrays.”This will buy you binary search capability.If you wanted the best performance for your priority queue,would you need to modify the OrdArray class?15.A priority queue might be used to holda.passengers to be picked up by a taxi from different parts of the city.b.keystrokes made at a computer keyboard.c.squares on a chessboard in a game program.d.planets in a solar system simulation.Chapter4,Stacks and QueuesAnswers to Questions1.102.bst-In-First-Out;and First-In-First-Out4.False.It’s the other way around.5.b6.It doesn’t move at all.7.458.False.They take O(1)time.9.c10.O(N)11.c12.True13.b14.Yes,you would need a method to find the minimum value.15.aQuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.Questions2451.Which of the following is not true?A reference to a class objecta.can be used to access public methods in the object.b.has a size dependant on its class.c.has the data type of the class.d.does not hold the object itself.2.Access to the links in a linked list is usually through the_________link.3.When you create a reference to a link in a linked list,ita.must refer to the first link.b.must refer to the link pointed to by current.c.must refer to the link pointed to by next.d.can refer to any link you want.4.How many references must you change to insert a link in the middle of a singly linked list?5.How many references must you change to insert a link at the end of a singly linked list?6.In the insertFirst()method in the linkList.java program(Listing5.1),the statement newLink.next=first;means thata.the next new link to be inserted will refer to first.b.first will refer to the new link.c.the next field of the new link will refer to the old first link.d.newLink.next will refer to the new first link in the list.7.Assuming current points to the next-to-last link in a singly linked list,what statement will delete the last link from the list?8.When all references to a link are changed to refer to something else,what happens to the link?9.A double-ended lista.can be accessed from either end.b.is a different name for a doubly linked list.c.has pointers running both forward and backward between links.d.has its first link connected to its last link.246CHAPTER5Linked Lists10.A special case often occurs for insertion and deletion routines when a list is ________.11.Assuming a copy takes longer than a comparison,is it faster to delete an item with a certain key from a linked list or from an unsorted array?12.How many times would you need to traverse a singly linked list to delete the item with the largest key?13.Of the lists discussed in this chapter,which one would be best for implementing a queue?14.Which of the following is not true?Iterators would be useful if you wanted toa.do an insertion sort on a linked list.b.insert a new link at the beginning of a list.c.swap two links at arbitrary locations.d.delete all links with a certain key value.15.Which do you think would be a better choice to implement a stack:a singly linked list or an array?Chapter5,Linked ListsAnswers to Questions1.b2.first3.d4.25.16.c7.current.next=null;8.Java’s garbage collection process destroys it.Chapter5,Linked Lists7419.a10.empty11.a linked list12.once,if the links include a previous reference13.a double-ended list14.bually,the list.They both do push()and pop()in O(1)time,but the list uses memory more efficiently.QuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.If the user enters10in the triangle.java program(Listing6.1),what is the maximum number of“copies”of the triangle()method(actually just copies of its argument)that exist at any one time?2.Where are the copies of the argument,mentioned in question1,stored?a.in a variable in the triangle()methodb.in a field of the TriangleApp classc.in a variable of the getString()methodd.on a stack3.Assume the user enters10as in question1.What is the value of n when the triangle()method first returns a value other than1?4.Assume the same situation as in question1.What is the value of n when the triangle()method is about to return to main()?5.True or false:In the triangle()method,the return values are stored on thestack.6.In the anagram.java program(Listing6.2),at a certain depth of recursion,a version of the doAnagram()method is working with the string“led”.When this method calls a new version of itself,what letters will the new version be working with?7.We’ve seen that recursion can take the place of a loop,as in the loop-oriented orderedArray.java program(Listing2.4)and the recursive binarySearch.java program(Listing6.3).Which of the following is not true?a.Both programs divide the range repeatedly in half.b.If the key is not found,the loop version returns because the rangebounds cross,but the recursive version occurs because it reaches thebottom recursion level.c.If the key is found,the loop version returns from the entire method, whereas the recursive version returns from only one level of recursion.d.In the recursive version the range to be searched must be specified in the arguments,while in the loop version it need not be.310CHAPTER6Recursion8.In the recFind()method in the binarySearch.java program(Listing6.3),what takes the place of the loop in the non-recursive version?a.the recFind()methodb.arguments to recFind()c.recursive calls to recFind()d.the call from main()to recFind()9.The binarySearch.java program is an example of the_________approach to solving a problem.10.What gets smaller as you make repeated recursive calls in the redFind() method?11.What becomes smaller with repeated recursive calls in the towers.java program (Listing6.4)?12.The algorithm in the towers.java program involvesa.“trees”that are data storage devices.b.secretly putting small disks under large disks.c.changing which columns are the source and destination.d.moving one small disk and then a stack of larger disks.13.Which is not true about the merge()method in the merge.java program(Listing6.5)?a.Its algorithm can handle arrays of different sizes.b.It must search the target array to find where to put the next item.c.It is not recursive.d.It continuously takes the smallest item irrespective of what array it’s in.14.The disadvantage of mergesort is thata.it is not recursive.b.it uses more memory.c.although faster than the insertion sort,it is much slower than quicksort.d.it is complicated to implement.15.Besides a loop,a___________can often be used instead of recursion. Chapter6,RecursionAnswers to Questions1.102.d3.24.105.false6.“ed”7.b8.c9.divide-and-conquer10.the range of cells to search11.the number of disks to transfer12.c13.b14.b15.stackQuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.The Shellsort works bya.partitioning the array.b.swapping adjacent elements.c.dealing with widely separated elements.d.starting with the normal insertion sort.2.If an array has100elements,then Knuth’s algorithm would start with an interval of________.Questions3613.To transform the insertion sort into the Shellsort,which of the following do you not do?a.Substitute h for1.b.Insert an algorithm for creating gaps of decreasing width.c.Enclose the normal insertion sort in a loop.d.Change the direction of the indices in the inner loop.4.True or false:A good interval sequence for the Shellsort is created by repeatedly dividing the array size in half.5.Fill in the big O values:The speed of the Shellsort is more than_______but less than________.6.Partitioning isa.putting all elements larger than a certain value on one end of the array.b.dividing an array in half.c.partially sorting parts of an array.d.sorting each half of an array separately.7.When partitioning,each array element is compared to the_______.8.In partitioning,if an array element is equal to the answer to question7,a.it is passed over.b.it is passed over or not,depending on the other array element.c.it is placed in the pivot position.d.it is swapped.9.True or false:In quicksort,the pivot can be an arbitrary element of the array.10.Assuming larger keys on the right,the partition isa.the element between the left and right subarrays.b.the key value of the element between the left and right subarrays.c.the left element in the right subarray.d.the key value of the left element in the right subarray.11.Quicksort involves partitioning the original array and then_________.362CHAPTER7Advanced Sorting12.After a partition in a simple version of quicksort,the pivot may beed to find the median of the array.b.exchanged with an element of the right subarray.ed as the starting point of the next partition.d.discarded.13.Median-of-three partitioning is a way of choosing the_______.14.In quicksort,for an array of N elements,the partitionIt()method will examine each element approximately______times.15.True or false:You can speed up quicksort if you stop partitioning when the partition size is5and finish by using a different sort.Chapter7,Advanced SortingAnswers to Questions1.c2.403.d4.false5.O(N*logN),O(N2)6.a7.pivot8.d9.true10.c11.partitioning the resulting subarrays12.b13.pivot14.log2N15.trueQuestionsThese questions are intended as a self-test for readers.Answers may be found inAppendix C.1.Insertion and deletion in a tree require what big O time?2.A binary tree is a search tree ifa.every non-leaf node has children whose key values are less than(or equalto)the parent.b.every left child has a key less than the parent and every right child has akey greater than(or equal to)the parent.c.in the path from the root to every leaf node,the key of each node isgreater than(or equal to)the key of its parent.d.a node can have a maximum of two children.3.True or False:Not all trees are binary trees.4.In a complete binary tree with20nodes,and the root considered to be at level 0,how many nodes are there at level4?5.A subtree of a binary tree always hasa.a root that is a child of the main tree’s root.b.a root unconnected to the main tree’s root.c.fewer nodes than the main tree.d.a sibling with the same number of nodes.6.In the Java code for a tree,the______and the_______are generally separate classes.Questions4237.Finding a node in a binary search tree involves going from node to node, askinga.how big the node’s key is in relation to the search key.b.how big the node’s key is compared to its right or left children.c.what leaf node we want to reach.d.what level we are on.8.An unbalanced tree is onea.in which most of the keys have values greater than the average.b.whose behavior is unpredictable.c.in which the root or some other node has many more left children thanright children,or vice versa.d.that is shaped like an umbrella.9.Inserting a node starts with the same steps as_______a node.10.Suppose a node A has a successor node S.Then S must have a key that is larger than_____but smaller than or equal to_______.11.In a binary tree used to represent a mathematical expression,which of the following is not true?a.Both children of an operator node must be operands.b.Following a postorder traversal,no parentheses need to be added.c.Following an inorder traversal,parentheses must be added.d.In pre-order traversal a node is visited before either of its children.12.If a tree is represented by an array,the right child of a node at index n has an index of_______.13.True or False:Deleting a node with one child from a binary search tree involves finding that node’s successor.14.A Huffman tree is typically used to_______text.15.Which of the following is not true about a Huffman tree?a.The most frequently used characters always appear near the top of the tree.b.Normally,decoding a message involves repeatedly following a path fromthe root to a leaf.424CHAPTER8Binary Treesc.In coding a character you typically start at a leaf and work upward.d.The tree can be generated by removal and insertion operations on apriority queue.Chapter8,Binary TreesAnswers to Questions1.O(logN)2.b3.True4.55.c6.node,tree7.a8.cChapter8,Binary Trees7439.finding10.A,A’s left-child descendents11.d12.2*n+113.Falsepress15.cQuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.A2-3-4tree is so named because a node can havea.three children and four data items.b.two,three,or four children.c.two parents,three children,and four items.d.two parents,three items,and four children.2.A2-3-4tree is superior to a binary search tree in that it is________.3.Imagine a parent node with data items25,50,and75.If one of its child nodes had items with values60and70,it would be the child numbered__________.4.True or False:Data items are located exclusively in leaf nodes.514CHAPTER102-3-4Trees and External Storage5.Which of the following is not true each time a node is split?a.Exactly one new node is created.b.Exactly one new data item is added to the tree.c.One data item moves from the split node to its parent.d.One data item moves from the split node to its new sibling.6.A2-3-4tree increases its number of levels when________.7.Searching a2-3-4tree does not involvea.splitting nodes on the way down if necessary.b.picking the appropriate child to go to,based on data items in a node.c.ending up at a leaf node if the search key is not found.d.examining at least one data item in any node visited.8.After a non-root node of a2-3-4tree is split,does its new right child contain the item previously numbered0,1,or2?9.A4-node split in a2-3-4tree is equivalent to a_______in a red-black tree.10.Which of the following statements about a node-splitting operation in a2-3 tree(not a2-3-4tree)is not true?a.The parent of a split node must also be split if it is full.b.The smallest item in the node being split always stays in that node.c.When the parent is split,child2must always be disconnected from itsold parent and connected to the new parent.d.The splitting process starts at a leaf and works upward.11.What is the big O efficiency of a2-3tree?12.In accessing data on a disk drive,a.inserting data is slow but finding the place to write data is fast.b.moving data to make room for more data is fast because so many items can be accessed at once.c.deleting data is unusually fast.d.finding the place to write data is comparatively slow but a lot of data can be written quickly.13.In a B-tree each node contains_______data items.Questions51514.True or False:Node splits in a B-tree have similarities to node splits in a2-3 tree.15.In external storage,indexing means keeping a file ofa.keys and their corresponding blocks.b.records and their corresponding blocks.c.keys and their corresponding records.st names and their corresponding keys.Chapter9,Red-Black TreesAnswers to Questions1.in order(or inverse order)2.b3.False4.d5.b6.rotations,changing the colors of nodes7.red8.a9.left child,right child10.d11.a node,its two children12.b13.True14.a15.TrueQuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.574CHAPTER11Hash Tablesing big O notation,say how long it takes(ideally)to find an item in a hash table.2.A__________transforms a range of key values into a range of index values.3.Open addressing refers toa.keeping many of the cells in the array unoccupied.b.keeping an open mind about which address to use.c.probing at cell x+1,x+2,and so on until an empty cell is found.d.looking for another location in the array when the one you want is occupied.ing the next available position after an unsuccessful probe is called_____________.5.What are the first five step sizes in quadratic probing?6.Secondary clustering occurs becausea.many keys hash to the same location.b.the sequence of step lengths is always the same.c.too many items with the same key are inserted.d.the hash function is not perfect.7.Separate chaining involves the use of a_____________at each location.8.A reasonable load factor in separate chaining is________.9.True or False:A possible hash function for strings involves multiplying each character by an ever-increasing power.10.The best technique when the amount of data is not well known isa.linear probing.b.quadratic probing.c.double hashing.d.separate chaining.11.If digit folding is used in a hash function,the number of digits in each group should reflect_____________.12.True or False:In linear probing an unsuccessful search takes longer than a successful search.Questions57513.In separate chaining the time to insert a new itema.increases linearly with the load factor.b.is proportional to the number of items in the table.c.is proportional to the number of lists.d.is proportional to the percentage of full cells in the array.14.True or False:In external hashing,it’s important that the records don’t become full.15.In external hashing,all records with keys that hash to the same value are located in___________.Chapter11,Hash TablesAnswers to Questions1.O(1)2.hash function3.d4.linear probing5.1,4,9,16,256.b7.linked listChapter11,Hash Tables7458.1.09.True10.d11.the array size12.False13.a14.False15.the same blockQuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.What does the term complete mean when applied to binary trees?a.All the necessary data has been inserted.b.All the rows are filled with nodes,except possibly the bottom one.c.All existing nodes contain data.d.The node arrangement satisfies the heap condition.2.What does the term weakly ordered mean when applied to heaps?3.A node is always removed from the__________.4.To“trickle up”a node in a descending heap meansa.to repeatedly exchange it with its parent until it’s larger than its parent.b.to repeatedly exchange it with its child until it’s larger than its child.c.to repeatedly exchange it with its child until it’s smaller than its child.d.to repeatedly exchange it with its parent until it’s smaller than its parent.5.A heap can be represented by an array because a heap。

数据结构总复习题(JAVA)

数据结构总复习题(JAVA)

一、填空题1. 栈和队列的共同特点是(只允许在端点处插入和删除元素)。

2. 在深度为5的满二叉树中,叶子结点的个数为(31)3. 算法分析的目的是(分析算法的效率以求改进)。

4. 由两个栈共享一个存储空间的好处是(节省存储空间,降低上溢发生的机率)。

5.串的长度是(串中所含字符的个数)。

6.设有两个串p和q,求q在p中首次出现位置的运算称做(模式匹配)7. N个顶点的连通图中边的条数至少为(N-1)。

8.N个顶点的强连通图的边数至少有(N)。

9.对长度为n的线性表进行顺序查找,在最坏情况下所需要的比较次数为(N)。

P25910.假设线性表的长度为n,则在最坏情况下,冒泡排序需要的比较次数为(n(n-1)/2)。

P29211. 在n个结点的单链表中要删除已知结点*p,需找到它的前驱结点的地址,其时间复杂度为O(n)。

12. 在具有n个单元的循环队列中,队满时共有 n-1 个元素。

13. 有向图G用邻接表矩阵存储,其第i行的所有元素之和等于顶点i的出度。

14. 用Dijkstra算法求某一顶点到其余各顶点间的最短路径是按路径长度递增的次序来得到最短路径的。

15. 在图形结构中,每个结点的前驱结点数和后续结点数可以任意多个。

16.在一个循环队列中,队首指针指向队首元素的前一个位置。

17.在顺序表中插入或删除一个元素,需要平均移动表中一半元素,具体移动的元素个数与表长和该元素在表中的位置有关。

18. 线性表中结点的集合是有限的,结点间的关系是一对一的。

19.数据结构被形式地定义为(D, R),其中D是数据元素的有限集合,R是D上的关系有限集合。

20. 线性结构中元素之间存在一对一关系,树形结构中元素之间存在一对多关系,图形结构中元素之间存在多对多关系。

21. 一个算法的效率可分为时间效率和空间效率。

22. 在顺序表中访问任意一结点的时间复杂度均为 O(1) ,因此,顺序表也称为随机存取的数据结构。

23. 在n个结点的单链表中要删除已知结点*p,需找到它的前驱结点的地址,其时间复杂度为O(n)。

数据结构与算法分析 java 习题答案

数据结构与算法分析 java 习题答案

数据结构与算法分析 java 习题答案数据结构与算法分析 Java 习题答案在学习数据结构与算法的过程中,习题练习是非常重要的一环。

通过解答习题,我们可以巩固所学的知识,加深对数据结构与算法的理解,提高编程能力。

下面我们将对一些常见的 Java 习题进行分析,并给出相应的答案。

1. 请编写一个函数,实现对一个整型数组的冒泡排序。

```javapublic void bubbleSort(int[] arr) {int n = arr.length;for (int i = 0; i < n-1; i++) {for (int j = 0; j < n-i-1; j++) {if (arr[j] > arr[j+1]) {int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}}```2. 给定一个整型数组和一个目标值,返回数组中和为目标值的两个数的下标。

```javapublic int[] twoSum(int[] nums, int target) {Map<Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {int complement = target - nums[i];if (map.containsKey(complement)) {return new int[] { map.get(complement), i };}map.put(nums[i], i);}throw new IllegalArgumentException("No two sum solution"); }```3. 实现一个栈,包括 push、pop、top 和 getMin 函数。

Java常见数据结构面试题(带答案)

Java常见数据结构面试题(带答案)

Java常见数据结构⾯试题(带答案)1.栈和队列的共同特点是(只允许在端点处插⼊和删除元素)4.栈通常采⽤的两种存储结构是(线性存储结构和链表存储结构)5.下列关于栈的叙述正确的是(D)A.栈是⾮线性结构B.栈是⼀种树状结构C.栈具有先进先出的特征D.栈有后进先出的特征6.链表不具有的特点是(B)A.不必事先估计存储空间 B.可随机访问任⼀元素C.插⼊删除不需要移动元素D.所需空间与线性表长度成正⽐7.⽤链表表⽰线性表的优点是(便于插⼊和删除操作)8.在单链表中,增加头结点的⽬的是(⽅便运算的实现)9.循环链表的主要优点是(从表中任⼀结点出发都能访问到整个链表)10.线性表L=(a1,a2,a3,……ai,……an),下列说法正确的是(D)A.每个元素都有⼀个直接前件和直接后件B.线性表中⾄少要有⼀个元素C.表中诸元素的排列顺序必须是由⼩到⼤或由⼤到⼩D.除第⼀个和最后⼀个元素外,其余每个元素都有⼀个且只有⼀个直接前件和直接后件11.线性表若采⽤链式存储结构时,要求内存中可⽤存储单元的地址(D)A.必须是连续的B.部分地址必须是连续的C.⼀定是不连续的D.连续不连续都可以12.线性表的顺序存储结构和线性表的链式存储结构分别是(随机存取的存储结构、顺序存取的存储结构)13.树是结点的集合,它的根结点数⽬是(有且只有1)14.在深度为5的满⼆叉树中,叶⼦结点的个数为(31)15.具有3个结点的⼆叉树有(5种形态)16.设⼀棵⼆叉树中有3个叶⼦结点,有8个度为1的结点,则该⼆叉树中总的结点数为(13)17.已知⼆叉树后序遍历序列是dabec,中序遍历序列是debac,它的前序遍历序列是(cedba)18.已知⼀棵⼆叉树前序遍历和中序遍历分别为ABDEGCFH和DBGEACHF,则该⼆叉树的后序遍历为(DGEBHFCA)19.若某⼆叉树的前序遍历访问顺序是abdgcefh,中序遍历访问顺序是dgbaechf,则其后序遍历的结点访问顺序是(gdbehfca)20.数据库保护分为:安全性控制、完整性控制、并发性控制和数据的恢复。

数据结构与算法分析 java 习题答案

数据结构与算法分析 java 习题答案

数据结构与算法分析 java 习题答案数据结构与算法分析 Java 习题答案在学习数据结构与算法分析的过程中,习题练习是非常重要的一部分。

通过习题的解答,我们可以巩固所学的知识,加深对算法的理解,并提升解决问题的能力。

本文将为大家提供一些数据结构与算法分析的 Java 习题答案,希望能够对大家的学习有所帮助。

1. 习题一:编写一个 Java 程序,实现冒泡排序算法。

```javapublic class BubbleSort {public static void bubbleSort(int[] arr) {int n = arr.length;for (int i = 0; i < n-1; i++) {for (int j = 0; j < n-i-1; j++) {if (arr[j] > arr[j+1]) {int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}}public static void main(String[] args) {int[] arr = {64, 34, 25, 12, 22, 11, 90};bubbleSort(arr);System.out.println("排序后的数组:");for (int i = 0; i < arr.length; i++) {System.out.print(arr[i] + " ");}}}```2. 习题二:编写一个 Java 程序,实现二分查找算法。

```javapublic class BinarySearch {public static int binarySearch(int[] arr, int target) { int left = 0;int right = arr.length - 1;while (left <= right) {int mid = left + (right - left) / 2;if (arr[mid] == target) {return mid;}if (arr[mid] < target) {left = mid + 1;} else {right = mid - 1;}}return -1;}public static void main(String[] args) {int[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};int target = 12;int result = binarySearch(arr, target);if (result == -1) {System.out.println("目标元素不存在");} else {System.out.println("目标元素的索引为:" + result); }}}```3. 习题三:编写一个 Java 程序,实现链表的反转。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1 下列数据结构中,能用二分法进行查找的是__A____。

A、顺序存储的有序线性表B、线性链表C、二叉链表D、有序线性链表解析:二分法查找只合用于顺序存储的有序表。

在此所说的有序表是指线性表中的元素按值非递减罗列(即从小到大,但允许相邻元素值相等)。

2 在软件设计中,不属于过程设计工具的是__D____。

A、PDL(过程设计语言)B、PAD 图C、N-S 图D、DFD 图解析:软件设计工具包括:程序流程图、 N-S、PAD、HIPO,判定表, PDL(伪码)。

而 DFD(数据流图)属于结构化分析工具。

3 在 switch(expression)语句中, expression 的数据类型不能是__A____。

A、doubleB、charC、byteD、short解析:表达式expression 只能返回这个几种类型的值: int、byte、short 和 char。

多分支语句把表达式返回的值挨次与每一个 case 子句中的值相比较,如果遇到匹配的值,则执行该 case 子句后的语句序列。

4 下列叙述中,错误的是__D____。

A、父类不能替代子类B、子类能够替代父类C、子类继承父类D、父类包含子类5 通过继承实现代码复用:Java 中所有的类都是通过直接或者间接地继承 ng.Object 类得到的。

继承而得到的类称为子类,被继承的类称为父类。

子类不能继承父类中访问权限为 private 的成员变量和方法,子类可以重写父类的方法,及命名与父类同名的成员变量。

子类通过隐藏父类的成员变量和重写父类的方法,把父类的状态和行为改变为自身的状态和行为。

注意:子类中重写的方法和父类中被重写的方法要具有相同的名字,相同的参数表和相同的返回类型,只是函数体不同。

由于子类继承了父类所有的属性(私有的除外),所以子类对象可以作为父类对象使用。

程序中凡是使用父类对象的地方,都可以用子类对象来代替。

一个对象可以通过引用子类的实例来调用子类的方法。

java 运行时系统根据调用该方法的实例,来决定调用哪个方法。

对子类的一个实例,如果子类重写了父类的方法,则运行时系统调用子类的方法;如果子类继承了父类的方法(未重写),则运行时系统调用父类的方法。

6 自定义表格类中的 model 部份应实现的接口是___A___。

A、AbstractTableModelB、JTableC、TableModelD、TableModelable7 下列代码中,将引起编译错误的行是__B____。

1)public class Exercise{2) public static void main(String args[]){3) float f=0.0;4) f+=1.0;5) }6) }A、第 2 行B、第 3 行C、第 4 行D、第 6 行解析: float 定义变量赋值时,需要在数值后面加f 以标识它为浮点型,让系统知道该给它精确到多少位。

8 下列关于 Java 多线程并发控制机制的叙述中,错误的是___B___。

A、Java 中对共享数据操作的并发控制是采用加锁技术B、线程之间的交互,提倡采用 suspend()/resume()方法C、共享数据的访问权限都必须定义为 privateD、Java 中没有提供检测与避免死锁的专门机制,但应用程序员可以采用某些策略防止死锁的发生解析:1)Java 中对共享数据操作的并发控制是采用传统的封锁技术。

一个程序中单独的、并发的线程对同一个对象进行访问的代码段,称为临界区。

在Java 语言中,临界区可以是一个语句块或者是一个方法,并且用"synchronized"关键字标识。

Java 平台将每一个由synchronized(Object)语句指定的对象设置一个锁,称为对象锁。

2)共享数据的所有访问都必须作为临界区,使用"synchronized"进行加锁控制。

用"synchronized"保护的数据也必须是私有的,使线程不能直接访问这些数据,必须通过对象的方法。

3)Java 中没有检测与避免死锁的专门机制,因此彻底由程序进行控制,防止死锁的发生。

4)有时,某个线程进入"synchronized"块后,共享数据的状态并不一定满足线程的需要,它要等待其他线程将共享数据改变为它需要的状态后才干继续执行,但由于此时它占有了该对象的锁,其他线程无法对共享数据进行操作,为此 Java 引入 wait()和notify(),这两个方法使 ng.object 类的方法,使实现线程通信的两个方法。

9 下列操作中,不属于 Applet 安全限制的是___D___。

A、加载本B、读写本地文件系统C、运行本地可执行程序D、与同一个页面中的 Applet 通信10 在进行模块测试时,要为每一个被测试的模块此外设计两类模块:驱动模块和承接模块(桩模块)。

其中,驱动模块相当于被测试模块的主程序,它接收测试数据,并传给被测试模块,输出实际测试结果。

承接模块通常用于代替被测试模块调用的其他模块,其作用仅做少量的数据操作,是一个摹拟子程序,不势必子模块的所有功能带入。

11 Java 语言具有可移植性、高性能、茁壮性、安全性和独立于体系结构的__跨平台____ 特点。

解析: Java 语言是一种跨平台,适合于分布式计算环境的面向对象的编程语言。

具体来说,它具有如下特性:简单性、面向对象、分布式、解释型、可靠、安全、平台无关、可移植、高性能、多线程、动态性等。

12 在运行时,由 Java 解释器自动导入,而不用 import 语句引入的包是__ng____。

解析:因为包 ng 所包含的类和接口对所有实际的 Java 程序都是必要的,所以,它被自动导入所有的程序且它是 Java 最广泛使用的包。

13 下列程序的功能是创建了一个显示 5 个"Hello!"的线程并启动运行,请将程序补充完整。

public class ThreadTest extends Thread{public static void main(String args[]){ThreadTest t=new __ThreadTest()____ ;t.start();}public void run(){int i=0;while(true){System.out.println("Hello!");if (i++==4) break;}}解析: ThreadTest 继承 ng.Thread 类,重写了 run()方法,实现了 Java 中的线程。

ThreadTest t 定义了空的线程对象,下面 t.start()启动了这个线程,因此 ThreadTest t=new ______ ;就应该是实例化该线程对象,所以空格中应填ThreadTest()。

Swing 的顶层容器有: JApplet、JWindow、JDialog 和__JFrame____。

顶层容器: JFrame、JApplet、JDialog 和 JWindow 共 4 个。

中间容器: JPanel、JScrollPane、JSplitPane、JToolBar。

特殊容器: JInternalFrame、JLayeredPane、JRootPane。

基本控件: JButton、JComboBox、JList、JMenu、JSlider、JTextField。

不可编辑信息的构件: JLabel、JProgressBar、ToolTip、可编辑信息的构件: JColorChooser、JFileChooser、JFileChooser、JTable、JTextArea 所有的这些构件的分类都是按功能来划分的。

14 下列叙述中正确的是___D___。

A、一个逻辑数据结构只能有一种存储结构B、数据的逻辑结构属于线性结构,存储结构属于非线性结构C、一个逻辑数据结构可以有多种存储结构,且各种存储结构不影响数据处理的效率D、一个逻辑数据结构可以有多种存储结构,且各种存储结构影响数据处理的效率解析:普通来说,一种数据的逻辑结构根据需要可以表示成多种存储结构,常用的存储结构有顺序、链接、索引等存储结构。

而采用不同的存储结构,其数据处理的效率是不同的。

15 Java 的图形用户界面的最基本的组成部份就是构件(Component),构件是一个可以以图形化的方式显示在屏幕上并能与用户交互的对象,但构件不能独立地显示出来,必须将构件放在一定的容器中才可以显示出来。

解析:容器 Container 是一个类,因为容器本身也是一个构件,具有构件的所有性质,因此继承之Component 类。

16 下列叙述中,错误的是__A____。

A、File 类能够存储文件B、File 类能够读写文件C、File 类能够建立文件D、File类能够获取文件目录信息解析:文件类 File 是 java.io 包中的一个重要的非流类,它以一种与系统无关的方式表示一个文件对象的属性。

而目录在 Java 中作为一种特殊文件,即文件名的列表,通过类File 所提供的方法,可得到文件或者目录的描述信息(包括名字、路径、长度、可读、可写等),也可以生成新文件、目录、修改文件和目录,查询文件属性,重命名文件或者删除文件。

17 下列叙述中,正确的是___C___。

A、Reader 是一个读取字符文件的接口B、Reader 是一个读取数据文件的抽象类C、Reader 是一个读取字符文件的抽象类D、Reader 是一个读取字节文件的普通类解析: Java 中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种,所以一共四个):InputStream,OutputStream,Reader,Writer。

Java 中其他多种多样变化的流均是由它们派生出来的。

在这其中 InputStream 和 OutputStream 在早期的 Java 版本中就已经存在了,它们是基于字节流的,而基于字符流的 Reader 和 Writer 是后来加入作为补充的。

在这四个抽象类中,InputStream 和 Reader 定义了彻底相同的接口:int read()int read(char cbuf[])int read(char cbuf[], int offset, int length)而 OutputStream 和 Writer 也是如此:int write(int c)int write(char cbuf[])int write(char cbuf[], int offset, int length)18 用于输入压缩文件格式的 ZipInputStream 类所属包是___D___。

相关文档
最新文档